Created
January 29, 2016 10:32
-
-
Save okunishinishi/ac7017c0b05e1719780b 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"; | |
| function firefox(action) { | |
| var handler = firefox.handlers[action.index]; | |
| if (handler) { | |
| return new Promise(handler); | |
| } | |
| console.log('Done in FireFox'); | |
| return null; | |
| } | |
| function chrome(action) { | |
| var handler = chrome.handlers[action.index]; | |
| if (handler) { | |
| return new Promise(handler); | |
| } | |
| console.log('Done in Chrome'); | |
| return null; | |
| } | |
| function main() { | |
| function _resolve(action) { | |
| return Promise.resolve(action).then(function (action) { | |
| switch (action.turn) { | |
| case 'firefox': | |
| return firefox(action); | |
| case 'chrome': | |
| return chrome(action); | |
| default: | |
| throw new Error('Unknown turn:' + turn); | |
| } | |
| }).then(function (action) { | |
| if (action) { | |
| return _resolve(action); | |
| } else { | |
| return null; | |
| } | |
| }); | |
| } | |
| _resolve({index: 0, turn: 'firefox'}); | |
| } | |
| firefox.handlers = [ | |
| defineF0({ | |
| index: 0, | |
| turn: 'chrome' | |
| }), | |
| function (resolve, reject) { | |
| setTimeout(function () { | |
| console.log('This is F1'); | |
| resolve({ | |
| index: 1, | |
| turn: 'chrome' | |
| }); | |
| }, 1000); | |
| } | |
| ]; | |
| chrome.handlers = [ | |
| function (resolve, reject) { | |
| setTimeout(function () { | |
| console.log('This is C0'); | |
| resolve({ | |
| index: 1, | |
| turn: 'firefox' | |
| }); | |
| }, 1000) | |
| }, | |
| function (resolve, reject) { | |
| setTimeout(function () { | |
| console.log('This is C1'); | |
| resolve({ | |
| index: 'F2', | |
| turn: 'firefox' | |
| }); | |
| }, 1000) | |
| } | |
| ]; | |
| function defineF0(next) { | |
| return function (resolve, reject) { | |
| setTimeout(function () { | |
| console.log('This is F0'); | |
| resolve(next); | |
| }, 1000); | |
| }; | |
| } | |
| main(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment