Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kyriejoshua/promise-like
https://github.com/kyriejoshua/promise-like
es2015 es6 javascript promise
Last synced: 13 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kyriejoshua/promise-like
- Owner: kyriejoshua
- License: mit
- Created: 2018-04-01T13:12:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-23T22:21:06.000Z (over 1 year ago)
- Last Synced: 2024-07-30T20:04:23.049Z (5 months ago)
- Topics: es2015, es6, javascript, promise
- Language: TypeScript
- Size: 169 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Promise like
* A promise library like promise.
* node v11.2.0### Usage
* A simple example.
```javascript
const promiseA = new PromiseLike((resolve, reject) => {
setTimeout(() => {
console.info('A');
resolve('A');
}, 3000);
});console.info(PromiseLike.is(1), PromiseLike.is(PromiseLike.resolve()), PromiseLike.is(Promise.resolve()), promiseA) // expected output: false true false true
// then
const promiseA1 = promiseA.then(data => `${data}1`)
console.info(promiseA1)// race
const promiseB = new PromiseLike((resolve, reject) => {
setTimeout(() => {
console.info('B');
resolve('B');
}, 1000);
});
PromiseLike.race([promiseA, promiseB])
.then(res => console.info(res)) // expected output: 'B'// all
const promiseC = '3C'
PromiseLike.all([promiseA, promiseB, promiseC])
.then(res => console.info(res)) // expected output: Array [0, 'B', '3C', duringTime: '123ms']const createPromise = (timer, index) => () =>
new PromiseLike((resolve) => {
window.setTimeout(() => {
console.info(`promise${index}`, new Date())
resolve()
}, timer)
})// sequence
PromiseLike.sequence([createPromise(300, 1), createPromise(200, 2), createPromise(100, 3)])
.then(res => console.info(res)) // expected output: promise1, promise2, promise3// finally
const finallyResolved = PromiseLike.resolve(4).finally(() => {}) // expected output: fulfilled: 4
const finallyRejected = PromiseLike.reject(444).finally(() => {}) // expected output: rejected: 444
console.info('finallyResolved', `${finallyResolved.PromiseStatus}: ${finallyResolved.PromiseValue}`)
console.info('finallyRejected', `${finallyRejected.PromiseStatus}: ${finallyRejected.PromiseValue}`)
// 早先的版本里是同步调用时,结果如上是实时输出的
// 最新版改为异步调用,在异步进程后显示结果
window.setTimeout(() => {
console.info('finallyResolved', `${finallyResolved.PromiseStatus}: ${finallyResolved.PromiseValue}`)
console.info('finallyRejected', `${finallyRejected.PromiseStatus}: ${finallyRejected.PromiseValue}`)
}, 10)
```### Other
* [Promise/A+](https://promisesaplus.com)
* [ecma262](https://tc39.github.io/ecma262/#sec-promise-objects)