Reputation: 2224
Saw this as an interview question. What are special consequences of writing promises these ways? Some of these will do the same thing, but what is the special conditions I need to pay attention to?
If successful, do doSomethingElse();
since the inner function is done with currying, you can add special custom parameters.
doSomething().then(function () {
return doSomethingElse(a);
});
If successful:
doSomething().then(function () {
doSomethingElse(a);
});
doSomethingElse()
is a call back function, and this is same as 4(???)
doSomething().then(doSomethingElse());
callback function doSomethingElse()
, same as 3 (?)
doSomething().then(doSomethingElse);
Upvotes: 0
Views: 95
Reputation: 95644
Pay attention to when doSomethingElse
is called, what its parameter is, and whether that gets passed along to additional chained promises.
doSomething().then(function () {
return doSomethingElse(a);
});
This will:
doSomething()
immediately.doSomething()
to resolve.doSomethingElse()
with the custom argument a
, presumably from an outer scope. Anything chained after that will receive the return value of doSomethingElse(a)
.doSomething().then(function () {
doSomethingElse(a);
});
This will:
doSomething()
immediately.doSomething()
to resolve.doSomethingElse()
with the custom argument a
, presumably from an outer scope. Anything chained after that will receive undefined
, not the return value of doSomethingElse
.doSomething().then(doSomethingElse());
doSomething()
immediately.doSomethingElse()
immediately.doSomething()
to resolve.doSomethingElse
returned in step 2 with one parameter, the value returned by doSomething
in step 3. The return value of this function will be passed along to anything chained.doSomething().then(doSomethingElse);
doSomething()
immediately.doSomething()
to resolve.doSomethingElse
, very much like your example #1, except that the function is called with doSomething()
's value rather than your own a
value. The return value of doSomethingElse
is made available for chaining.Upvotes: 1
Reputation: 2970
1 vs 2
In case 1 - you can get the result of doSomethingElse(a)
in the next then
in you promise chain.
Like this:
doSomething().then(function () {
return doSomethingElse(a);
}).then(function (doSomethingElseWithAResult /* <-- here */) {
// Some code
});
In case 2 - you always get undefined
in the next then
in you promise chain:
doSomething().then(function () {
return doSomethingElse(a);
}).then(function (alwaysUndefined /* <-- here */) {
// Some code
});
1 vs 3
You could rewrite 3 like this:
doSomething().then(function (doSomethingResult) {
return doSomethingElse(doSomethingResult);
});
So 3 is close to 1, but in case 3 we do not provide special custom parameters to doSomethingElse
and we pass the result of doSomething
to doSomethingElse
instead of a
.
1 vs 4
The same as 1 vs 3, assuming that doSomethingElse
is a curried function at least 2 arguments.
2 vs 3
Remembering that we could rewrite 3 like this:
doSomething().then(function (doSomethingResult) {
return doSomethingElse(doSomethingResult);
});
The difference is that in case 2 we are not providing special custom parameters to doSomethingElse
(see 1 vs 3) and we don't pass the result of doSomethingElse
to the next then
in our promise chain (see 1 vs 2).
2 vs 4
The same as 2 vs 3, assuming that doSomethingElse
is a curried function at least 2 arguments.
3 vs 4
They are the same, assuming that doSomethingElse
is a curried function of at least 1 argument.
They are not the same if doSomethingElse
is not a curried function.
Upvotes: 2