promises-aplus / cancellation-spec

Discussion and drafts of a possible promise cancellation spec.
24 stars 5 forks source link

Use the constructor to construct cancellation #16

Open ysmood opened 9 years ago

ysmood commented 9 years ago

I have implemented my draft. It's very simple and should also work for progress. You can find the lib here yaku, and test it yourself. Here's the example:

let Promise = require('yaku')

// The `this` is the instance of the newly created promise.
let p = new Promise((resolve, reject) => {
    let tmr = setTimeout(resolve, 3000)

    let this.abort = () => {
        clearTimeout(tmr)
        reject(new Error('abort promise'))
    }

})

p.abort()

It's simple and flexible.