Last active
September 4, 2018 18:32
-
-
Save swvitaliy/57334ba02bd4dff25e077a5e4d2f4b08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var Q = require('q'); | |
var waitForA = null; | |
var waitForB = null; | |
function _a() { | |
if (waitForA) return waitForA; | |
console.info('call a'); | |
waitForA = Q.fcall(function() { | |
console.info('resolve a'); | |
return {a:1}; | |
}); | |
return waitForA; | |
} | |
function a() { | |
return _a().then(function(aRes) { | |
// wait until B will update the result of a() | |
if (!waitForB) return aRes; | |
return waitForB.then(function() { | |
waitForA = null; | |
return aRes; | |
}); | |
}); | |
} | |
function b() { | |
console.info('call b'); | |
return waitForB = Q.fcall(function() { | |
console.info('resolve b 1'); | |
return _a(); | |
}).then(function(obj) { | |
console.info('resolve b 2'); | |
waitForB = null; | |
obj.b = 2; | |
return {some_other:1}; | |
}); | |
} | |
Q.fcall(function() { | |
console.info('test 1'); | |
return [a(), b()]; | |
}).spread(function(aRes, bRes) { | |
console.info('result ', aRes, bRes); | |
console.info('---------------'); | |
console.info('test 2'); | |
return [b(), a()]; | |
}).spread(function(bRes, aRes) { | |
console.info('result ', aRes, bRes); | |
console.info('---------------'); | |
console.info('test 3'); | |
return a(); | |
}).then(function(aRes) { | |
console.info('result ', aRes); | |
console.info('---------------'); | |
console.info('test 4'); | |
return b(); | |
}).then(function(bRes) { | |
console.info('result ', bRes); | |
}).done(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment