// Promise.all is good for executing many promises at once | |
Promise.all([ | |
promise1, | |
promise2 | |
]); | |
// Promise.resolve is good for wrapping synchronous code | |
Promise.resolve().then(function () { | |
if (somethingIsNotRight()) { | |
throw new Error("I will be rejected asynchronously!"); | |
} else { | |
return "This string will be resolved asynchronously!"; | |
} | |
}); | |
// execute some promises one after the other. | |
// this takes an array of promise factories, i.e. | |
// an array of functions that RETURN a promise | |
// (not an array of promises themselves; those would execute immediately) | |
function sequentialize(promiseFactories) { | |
var chain = Promise.resolve(); | |
promiseFactories.forEach(function (promiseFactory) { | |
chain = chain.then(promiseFactory); | |
}); | |
return chain; | |
} | |
// Promise.race is good for setting a timeout: | |
Promise.race([ | |
new Promise(function (resolve, reject) { | |
setTimeout(reject, 10000); // timeout after 10 secs | |
}), | |
doSomethingThatMayTakeAwhile() | |
]); | |
// Promise finally util similar to Q.finally | |
// e.g. promise.then(...).catch().then(...).finally(...) | |
function finally (promise, cb) { | |
return promise.then(function (res) { | |
var promise2 = cb(); | |
if (typeof promise2.then === 'function') { | |
return promise2.then(function () { | |
return res; | |
}); | |
} | |
return res; | |
}, function (reason) { | |
var promise2 = cb(); | |
if (typeof promise2.then === 'function') { | |
return promise2.then(function () { | |
throw reason; | |
}); | |
} | |
throw reason; | |
}); | |
}; |
This comment has been minimized.
This comment has been minimized.
yup, got it :) |
This comment has been minimized.
This comment has been minimized.
Brilliant gist! Thanks! |
This comment has been minimized.
This comment has been minimized.
awesome |
This comment has been minimized.
This comment has been minimized.
thanks a lot man :) |
This comment has been minimized.
This comment has been minimized.
Your Article on PouchDB brought me here. Thank you very much :) |
This comment has been minimized.
This comment has been minimized.
^ ditto, great article and examples! |
This comment has been minimized.
This comment has been minimized.
thanks for the article and cool examples |
This comment has been minimized.
This comment has been minimized.
Great gist (got here from your also great article)! |
This comment has been minimized.
This comment has been minimized.
A very useful article and then this awesome gist ! |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Promise.race is the neatest timeout implementation ever. |
This comment has been minimized.
This comment has been minimized.
God bless you! |
This comment has been minimized.
This comment has been minimized.
Thanks for the Blog post on Promise, really enlightened my understanding on it! |
This comment has been minimized.
This comment has been minimized.
@nolanlawson nice gist, good stuff, thanks. In lieu of a PR, suggestion to streamline sequentialize with reduce, per my fork https://gist.github.com/bruceharris/8c9e6829c9f65ab33f31/revisions |
This comment has been minimized.
This comment has been minimized.
great article, keep up the good work, thanks :) |
This comment has been minimized.
This comment has been minimized.
I knew when I started learning promises a few days ago that I needed them and that I just didn't get them. Your article certainly helped me a lot to 'begin-to-get' them. I think after several re-reads I will finally 'get' them. ... ps |
This comment has been minimized.
This comment has been minimized.
Great article on pouchdb, really helped me out a lot. I have a question on the promise factory and chain example. Is there any way that I can I get the result from each step? Similar to how a Q.all() would operate. I'll clarify with my use case - I have a list of Nuget Packages that I am trying to delete, for each package I need to make an HTTP DELETE request. I initially used Q.all() but soon realised that this was sending all 500 requests at once - this is not ideal. Using your promise factories example I was able to add a delay between each request - very useful. But unfortunately the data returned in the HTTP request resolve() is lost. My Code for reference Q.when(genericKlondikeQuery(query, 'GET')).then(function (parsed) {
// logic for cleaning up the results
// ....
function packageDeletionFactory(id, version) {
return Q.delay(250).then(function () {
console.log('Deleting Package: ' + id + '/' + version);
return genericKlondikeQuery('/api/packages/' + id + '/' + version, 'DELETE');
});
}
// Promise Chain
var result = Q();
hits.forEach(function (element) {
result = result.then(function () {
return packageDeletionFactory(element.id, element.version);
});
});
return result;
}).then(function (results) {
// results == the result from the last chain link
done();
}).catch(console.log.bind(console)); |
This comment has been minimized.
This comment has been minimized.
@nolanlawson Can you explain how the above factory promise is working in depth. It will help many developers. |
This comment has been minimized.
This comment has been minimized.
@nolanlawson Thanks. |
This comment has been minimized.
This comment has been minimized.
Great Article! |
This comment has been minimized.
This comment has been minimized.
Here's a tip for if you want to use Promise.all({
foo: doSomething(),
bar: doSomethingElse(),
quux: doAnotherThing().then(q => q.property)
}).then(values => {
const { foo, bar, quux } = values
// now you have the result of foo, bar, and quux (including the transformation on quux from the "then" that it had)
}) |
This comment has been minimized.
This comment has been minimized.
nice article! |
This comment has been minimized.
This comment has been minimized.
Great article, still a rookie to understand promises and make useful application of the concepts. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
Is there a extra
)
on line 12?