/promising

:ring: Promises/A+ 规范的简单实现

Primary LanguageJavaScriptMIT LicenseMIT

Promising

Build Status

You are promising!

Introduction

Promises/A+ logo

Promising is a simple and tiny implementation of Promises/A+ specification. The standard tries to focus on providing an then method, by which we can interact with a promise. So it does not involve how to create, fulfill or reject a promise.

Can I use it?

Promising provides promise constructor and these instance/static methods:

  • Promising()
  • Promising.resolve()
  • Promising.reject()
  • Promising.prototype.then()
  • Promising.prototype.catch()

Promising does not provide these static methods:

  • Promising.all()
  • Promising.race()

As it does not provide other useful features, and ES6 has already embrace the native Promise object, you should not use it. But you can read the source code for the detail:

😊 Just have fun trying it out, maybe you will find something interesting! (Releases)

Usage

Here is a simple example (only in browser):

let getURL = (url) => {
  return new Promising((resolve, reject) => {
    let req = new XMLHttpRequest()
    req.open('GET', url, true)
    // success handler
    req.onload = () => {
      let code = req.status
      if ((code >= 200 && code < 300) || code == 304) {
        resolve(req.responseText)
      } else {
        reject(req.statusText)
      }
    }
    // error handler
    req.onerror = () => {
      reject(Error(req.statusText))
    }
    req.send()
  })
}

let url = 'http://httpbin.org/get'

getURL(url).then((res) => {
  console.log(res)
}).catch((err) => {
  console.log(err)
})

Another example about how to use the helper with Mocha and Chai:

// test.js
const { mayBeResolved, mayBeRejected } = require('./helper')

describe('testing static methods: ', () => {
  it('should be resolved', () => {
    mayBeResolved(Promising.resolve(42)).then((value) => {
      expect(value).to.equal(42)
    })
  })
  it('should be rejected', () => {
    mayBeRejected(Promising.reject('oops')).catch(reason => {
      expect(reason).to.equal('oops')
    })
  })
})

License

The MIT License (MIT)