I've found lots of solutions for this, typically something like
const serial = funcs =>
funcs.reduce((promise, func) =>
promise.then(result =>
func().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
)
I'm trying to map an array of promises and run them one after another,
serial(Object.keys(tables).map(key =>
websocketExecute(store,dropTableSQL(tables[key]),null)))
.then(data => {console.log(data);success(data)})
They all run however I get an error TypeError: func is not a function
And the final then isn't resolved..
Any idea how I run a final .then() on a list of promises?
Your function serial
expects its argument to be an array of functions that return Promises
however,
Object.keys(tables).map(key => websocketExecute(store,dropTableSQL(tables[key]),null))
returns an array of the results of calling
websocketExecute(store,dropTableSQL(tables[key]),null)
Which is not likely to be a function returning a promise, more like some result
What you'll want to do is:
serial(Object.keys(tables).map(key => () => websocketExecute(store,dropTableSQL(tables[key]),null)))
.then(data => {console.log(data);success(data)})
Assuming websocketExecute
returns a Promise
So now, the array returned by .map
is an array of
() => websocketExecute(store,dropTableSQL(tables[key]),null)
Which will get called in turn in .reduce