@debonet/es6pledges
Version:
Cancellable Promises for Javascript ES6
64 lines (54 loc) • 1.54 kB
JavaScript
const Pledge = require( "../src/es6pledges" );
function fpDelay( dtm, s ){
let timeout;
return new Pledge(
async (fResolve, fReject) => {
timeout = setTimeout(()=> {
console.log("FININSHED!");
fResolve( `[F:${dtm}]${s}` );
}, dtm);
},
( resolve, reject, status, reason, ...extras ) => {
(status?resolve:reject)(`[C:${reason}]${s}`);
console.log("CANCELLEED!");
clearTimeout( timeout );
return true;
}
);
}
// .then(()=>fpDelay( 3000 ))
// .then((x)=>"foobar"+x)
// .then((x)=>fpDelay( 3000, x ))
// .then((x)=>fpDelay( 3000, x ))
// .then((x)=>"foobar"+x)
//const p2 = Pact.all([ ])
//const p2 = Pledge.all([ ])
//const p2 = Pledge.all([ ])
// .then(x=>{console.log("foo",x); return 4;});
//const p2 = Pledge.resolve(100)
const p2 = fpDelay( 3000, "*" )
.then((x)=>fpDelay( 3001, x ))
.then((x)=>fpDelay( 3001, x ))
.then((x)=>fpDelay( 3001, x ))
//const p2 = Pledge.resolve("[S:100]")
.then((x)=>Pledge.reject("failed"+x))
.catch((x)=>Pledge.reject("failed"+x))
.then((x)=>fpDelay( 3001, x ))
.then((x)=>fpDelay( 3001, x ))
.catch((x)=>fpDelay( 3001, x ))
.then((x)=>Pledge.reject("failed"+x))
.then((x)=>fpDelay( 3001, x ))
/*
.then((x)=>"foobar"+x)
.then((x)=>fpDelay( 3000, x ))
.then((x)=>"bar"+x)
.then((x)=>fpDelay( 3000, x ))
.catch((x)=>({ "foo":x}))
.then((x)=>({ "bar":x}))
*/
.then((x)=>console.log("SUCCESS OUTPUT", x))
.catch((x)=>console.log("FAILURE OUTPUT", x))
setTimeout(()=>{
console.log("----------------------");
p2.release( true, "ok" );
}, 100);