Hope.js is a implementation for Promise/A+ specification which pass Promises/A+ Compliance Test Suite.
install
npm install @yangyuhe/hope --save
use
- with typescript
import {Hope} from "hope";
let hope=new Hope((resolve,reject)=>{
resolve("success");
}).then(res=>{
console.log(res);//output "success"
});
- with commonjs
let Hope=require("hope").Hope;
let hope=new Hope((resolve,reject)=>{
resolve("success");
}).then(res=>{
console.log(res);//output "success"
});
- in browser
import script of hope.js
<script src="...path/hope.js"></script>
<script>
let hope=new Hope((resolve,reject)=>{
resolve("success");
}).then(res=>{
console.log(res);//output "success"
});
</script>
API
Hope.js 's API is the same as normal Promise's api in Browser except with one extra api:
Hope.any(hope:Hope[]):Hope
the any method will resolve if any of hopes resolved and will reject if all of hopes rejected. when resolve, the parameter will be the first resolved value and when reject, then parameter will be a array which contains all reasons rejected in order of hopes passed in.