Last active
August 29, 2015 14:05
-
-
Save wryk/b20d6a5b7950ab82662a to your computer and use it in GitHub Desktop.
promise stuff
This file contains 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
import cast from 'cast' | |
/** | |
* @param {*} values | |
* @return {Promise} | |
* @resolve {*} | |
* @reject {Error} | |
**/ | |
export default function all (values) { | |
return new Promise(function (resolve, reject) { | |
var counter = 0 | |
var result = [] | |
promises | |
.map(cast) | |
.forEach(handle) | |
function handle (promise, index) { | |
promise.then(push, reject) | |
function push (value) { | |
result[index] = value | |
if (++counter === promises.length) { | |
resolve(result) | |
} | |
} | |
} | |
}) | |
} |
This file contains 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
import is from 'is' | |
import resolve from 'resolve' | |
/** | |
* @param {*} value | |
* @return {Promise} | |
* @resolve {*} | |
**/ | |
export default function cast (value) { | |
return is(value) ? value : resolve(value) | |
} |
This file contains 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
/** | |
* @param {*} value | |
* @return {Boolean} | |
**/ | |
export default function is (value) { | |
return value instanceof Promise | |
} |
This file contains 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
/** | |
* @param {Array<*>} values | |
* @return {Promise} | |
* @resolve {*} | |
* @reject {Error} | |
**/ | |
export default function race (values) { | |
return new Promise(function (resolve, reject) { | |
values | |
.map(cast) | |
.forEach(handle) | |
function handle (promise) { | |
promise.then(resolve, reject) | |
} | |
}) | |
} |
This file contains 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
/** | |
* @param {Error} reason | |
* @return {Promise} | |
* @reject {Error} | |
**/ | |
export default function reject (reason) { | |
return new Promise(function (_, reject) { | |
reject(reason) | |
}) | |
} |
This file contains 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
/** | |
* @param {*} value | |
* @return {Promise} | |
* @resolve {*} | |
**/ | |
export default function resolve (value) { | |
return new Promise(function (resolve) { | |
resolve(value) | |
}) | |
} |
This file contains 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
import cast from 'cast' | |
export default function some (values, number) { | |
return new Promise(function (resolve, reject) { | |
var counter = 0 | |
var result = [] | |
values | |
.map(cast) | |
.forEach(handle) | |
function handle (promise) { | |
promise.then(push, reject) | |
} | |
function push (value) { | |
result.push(value) | |
if (++counter === number) resolve(result) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment