Skip to content

Instantly share code, notes, and snippets.

@DaniilBabanin
Last active November 15, 2016 15:46
Show Gist options
  • Save DaniilBabanin/20c33d50cb981cf3dbb8d743612d25aa to your computer and use it in GitHub Desktop.
Save DaniilBabanin/20c33d50cb981cf3dbb8d743612d25aa to your computer and use it in GitHub Desktop.
Q.all with array map
return array.reduce(function(p, item) {
return p.then(function(){ return readFile(item); });
},Q());
Q.all(array.map(function (item) {
return doSomething(item); //function that returns promise
}))
.then(function (resolves) {
connection.response.items = resolves;
deferred.resolve();
})
.catch(function (err) {
deferred.reject(err);
})
;
Q.all(array.map(function (item) {
return doSomething.bind(this, item); //use bind this to run function in Q.ll insted of the map function
}))
.then(function (resolves) {
connection.response.items = resolves;
deferred.resolve();
})
.catch(function (err) {
deferred.reject(err);
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment