Understanding javascript promises; stacks and chaining -
i've been running couple of problems javascript promises, particularly stacked chains.
can explain me difference (if there any!) between these different implementations?
implementation 1
var serversidepromisechain; serversidepromisechain = async().then(function(response) { console.log('1', response); return response; }).then(function(response) { console.log('2', response); return true; }).then(function(response) { console.log('3', response); // response expected 'true' return async3(); }).then(function(response) { console.log('4', response); return async4(); }) implementation 2
var serversidepromisechain; serversidepromisechain = async().then(function(response) { console.log('1', response); return response; }); serversidepromisechain.then(function(response) { console.log('2', response); return true; }) serversidepromisechain.then(function(response) { console.log('3', response); // response expected 'true' return async3(); }) serversidepromisechain.then(function(response) { console.log('4', response); return async4(); }) implementation 3
var serversidepromisechain; serversidepromisechain = async().then(function(response) { console.log('1', response); return response; }); serversidepromisechain = serversidepromisechain.then(function(response) { console.log('2', response); return true; }) serversidepromisechain = serversidepromisechain.then(function(response) { console.log('3', response); // response expected 'true' return async3(); }) serversidepromisechain = serversidepromisechain.then(function(response) { console.log('4', response); return async4(); }) does fact part of chain returns value ('true' in step 2) change behavior? promises require returned values async promises keep behavior?
you illustrating different between chaining , branching. chaining wil sequence multiple async operations 1 starts when prior 1 finishes , can chain arbitrary number of items sequence 1 after other.
branching hooks multiple async operations in flight @ same time when 1 trigger operation completes.
implementations 1 , 3 same. chained. implementation 3 uses temporary variable chain, whereas implementation 1 uses return value .then() directly. no difference in execution. these .then() handlers called in serial fashion.
implementation 2 different. branched, not chained. because subsequent .then() handlers attached exact same serversidepromisechain promise, wait first promise resolved , subsequent async operations in flight @ same time (not serially in other 2 options).
it may helpful in understand dive 1 level down how works promises.
when (scenarios 1 , 3):
p.then(...).then(...) what happens follows:
- the interpreter takes
pvariable, finds.then()method on , calls it. - the
.then()method stores callback passed , returns new promise object. not call callback @ time. new promise object tied both original promise object , callback stored. not resolve until both satisfied. - then second
.then()handler on newly returned promise called. again,.then()handler on promise stores.then()callbacks , not yet executed. - then sometime in future, original promise
pgets resolved own async operation. when gets resolved, callsresolvehandlers stores. 1 of handlers callback first.then()handler in above chain. if callback runs completion , returns either nothing or static value (e.g. doesn't return promise itself), resolve promise created return after.then()first called. when promise resolved, call resolve handlers installed second.then()handler above , on.
when (scenario 2):
p.then(); p.then(); the 1 promise p here has stored resolve handlers both .then() calls. when original promise p resolved, call both of .then() handlers. if .then() handlers contain async code , return promises, these 2 async operations in flight @ same time (parallel-like behavior), not in sequence in scenario 1 , 3.
Comments
Post a Comment