Coder Social home page Coder Social logo

hybridables / redolent Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 135 KB

Simple promisify with sane defaults, works on node 0.10 if you provide custom Promise through options

Home Page: http://j.mp/1LLnFgE

License: MIT License

JavaScript 100.00%
async callback custom es2015 native promises promisify sane-defaults simple synchronous utils

redolent's People

Contributors

greenkeeperio-bot avatar tunnckocore avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

redolent's Issues

Resolve an interesting case

When for example, there is done argument, but is not called, and there is a result which is a promise.

something like

if (isAsyncFn && !called && isPromise(syncResult)) {
  resolve(syncResult)
}

https://github.com/hybridables/redolent/blob/master/index.js#L117-L124

edit:

this seems to fix it

      if ((!isAsyncFn && !called) || (isAsyncFn && !called && isPromise(syncResult))) {
        resolve(syncResult);
      }

i think we should handle this case, in that way. It is a lot better to auto-magically do it, instead of returning rejected promise that signals the user that he didn't called the done callback argument.

feat/bug: Reject when function is async but not `done` called nor Promise returned

Almost the same as #18

if (isAsyncFn && !called && !isPromise(syncResult)) {
  var msg = 'async function need to call `done/cb` callback argument or return a Promise'
  reject(new Error('redolent: ' + msg))
}

for example

var fn = redolent(function (foo, done) {
  console.log('foo is', foo)
  // done is not called nor Promise is returned,
  // so when `fn` is called it will return rejected promise 
})

fn(123).catch(console.log)

move to `hybridables`

Because it is one of the main mechanisms behind the new wave of hybrids and upcoming stable v1 releases cycle.

tests for generators

for example

var fn = redolent(function * () {return yield args})
fn(1, 2, 3).then(console.log) // => [1, 2, 3]

allow getting Promise Ctor through `.load` (?) method

so we can just

var PromiseCtor = redolent.load()
// or
var PromiseCtor = redolent.load(require('bluebird')) // or some other promise

working code from the bundle (index$10 is NativePromise, index$6 extend-shallow)

function loadPromise (Promize) {
  var promCtor = index$10 || Promize
  if (typeof promCtor !== 'function') {
    throw new TypeError('redolent: no native Promise support and no opts.Promise')
  }
  return promCtor
}

var index = function redolent (fn, opts) {
  if (typeof fn !== 'function') {
    throw new TypeError('redolent: expect `fn` to be a function')
  }

  opts = index$6({
    context: this,
    Promise: index$10
  }, opts);
  opts.Promise = loadPromise(opts.Promise)


  module.exports.Promise = opts.Promise
  redolent.Promise = opts.Promise
  index.Promise = opts.Promise

  return function () {
    opts.context = this || opts.context;
    opts.args = index$2(opts.args).concat(index$4(arguments));

    // if (typeof opts.Promise !== 'function') {
    //   throw new TypeError('redolent: no native Promise support and no opts.Promise')
    // }

    var promise = new opts.Promise(function (resolve, reject) {
      var called = false;

      function done (er, res) {
        called = true;
        if (er) {
          return reject(er)
        }
        if (arguments.length > 2) {
          res = index$4(arguments, 1);
        }
        return resolve(res)
      }

      var isAsyncFn = index$12(fn);

      opts.args = isAsyncFn ? opts.args.concat(done) : opts.args;
      var syncResult = fn.apply(opts.context, opts.args);

      if (!isAsyncFn && !called) {
        resolve(syncResult);
      }
    });

    return normalize(promise, index$10)
  }
};

function normalize (promise, isNativeSupported) {
  promise.___nativePromise = Boolean(isNativeSupported);
  promise.___customPromise = !promise.___nativePromise;
  return promise
}

index.load = loadPromise
module.exports = index;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.