Create a gist now

Instantly share code, notes, and snippets.

@domenic /example.js
Last active Aug 28, 2017

What would you like to do?
Promise chaining example
// `promise` is some operation that may succeed (fulfill) or fail (reject)
var newPromise = promise.then(
function () {
return delay(1000);
},
writeError
);
// If `promise` fulfills, `newPromise` will fulfill in 1000 ms.
// If `promise` rejects and writing to the error log succeeds,
// `newPromise` will fulfill: you transformed the rejection into fulfillment by handling it,
// similar to `try`/`catch`.
// If `promise` rejects and writing to the error log fails,
// `newPromise` will reject with the filesystem-related error: just as if
// code inside your `catch` block had thrown.
// Writes to errors.log, returning a promise that will be fulfilled if the write succeeds
// or rejected if the write fails.
function writeError(errMessage) {
var deferred = Q.defer();
fs.writeFile("errors.log", errMessage, function (err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
return deferred.promise;
}
// (or, using Q.nfcall:)
function writeError(errMessage) {
return Q.nfcall(fs.writeFile, "errors.log", errMessage);
}
// returns a promise that will be fulfilled in `ms` milliseconds
function delay(ms) {
var deferred = Q.defer();
setTimeout(deferred.resolve, ms);
return deferred.promise;
}
@StephanHoyer

This comment has been minimized.

Show comment Hide comment
@StephanHoyer

StephanHoyer Feb 8, 2013

I think, now i got it. Great example.

I think, now i got it. Great example.

@enzosaba

This comment has been minimized.

Show comment Hide comment
@enzosaba

enzosaba Oct 9, 2013

simple and instructive

enzosaba commented Oct 9, 2013

simple and instructive

@stefaneg

This comment has been minimized.

Show comment Hide comment
@stefaneg

stefaneg Dec 29, 2013

To be useful as an example it needs to be complete. This is incomplete since it provides no caller that uses the example functions, and a promise is not provided to which triggers "newPromise". So I disagree, this is bad example code IMHO.

To be useful as an example it needs to be complete. This is incomplete since it provides no caller that uses the example functions, and a promise is not provided to which triggers "newPromise". So I disagree, this is bad example code IMHO.

@jeffcogswell

This comment has been minimized.

Show comment Hide comment
@jeffcogswell

jeffcogswell Jan 4, 2014

@stefaneg -- agreed, this example isn't that great. Here's one that i made that introduces chaining in q. https://gist.github.com/jeffcogswell/8257755

@stefaneg -- agreed, this example isn't that great. Here's one that i made that introduces chaining in q. https://gist.github.com/jeffcogswell/8257755

@CodisRedding

This comment has been minimized.

Show comment Hide comment
@CodisRedding

CodisRedding Mar 21, 2014

Thanks @stefaneg. Your example helped out a lot. A combination of yours and the one that followed that shows how to pass args from promise to promise along with some rejection handling would be a perfect example.

Thanks @stefaneg. Your example helped out a lot. A combination of yours and the one that followed that shows how to pass args from promise to promise along with some rejection handling would be a perfect example.

@stianSjoli

This comment has been minimized.

Show comment Hide comment
@stianSjoli

stianSjoli Jul 10, 2015

I dont think the examples are good either. It would have been great to find one very simple (i find some regarding SQL of 50-100 lines for instance, and some with the Q.delay) working example (with requirements) that synchronise the output (I can't find a clear consensus of what is needed as a return value (some sort of Q object?) say an integer value from one async function to another async function that prints that value). It would have been great if this was in nodeJS instead of for instance coffee script.

I dont think the examples are good either. It would have been great to find one very simple (i find some regarding SQL of 50-100 lines for instance, and some with the Q.delay) working example (with requirements) that synchronise the output (I can't find a clear consensus of what is needed as a return value (some sort of Q object?) say an integer value from one async function to another async function that prints that value). It would have been great if this was in nodeJS instead of for instance coffee script.

@imakedon

This comment has been minimized.

Show comment Hide comment
@imakedon

imakedon Nov 8, 2015

Lame incomplete examples....

/promises.js:13
var newPromise = promise.then(
^
ReferenceError: promise is not defined
at Object. (/promises.js:13:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

imakedon commented Nov 8, 2015

Lame incomplete examples....

/promises.js:13
var newPromise = promise.then(
^
ReferenceError: promise is not defined
at Object. (/promises.js:13:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

@oprearocks

This comment has been minimized.

Show comment Hide comment
@oprearocks

oprearocks Mar 28, 2016

@imakedon, this might come a bit late, but the example clearly states this:

// promise is some operation that may succeed (fulfill) or fail (reject)

This means that the code is not 100% copy-paste and you should replace promise with whatever asynchronous operation you have at your disposal.

@imakedon, this might come a bit late, but the example clearly states this:

// promise is some operation that may succeed (fulfill) or fail (reject)

This means that the code is not 100% copy-paste and you should replace promise with whatever asynchronous operation you have at your disposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment