promise-resolver:将promises解析器方法转换为节点样式回调

  • V7_596737
    了解作者
  • 6.5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 12:07
    上传日期
承诺解决者 提供接受回调并返回promise而不需要Promise实现的灵活方法 安装 $ npm install --save promise-resolver 用法 var promiseResolver = require ( 'promise-resolver' ) ; function sayMyName ( name , cb ) { var deferred = promiseResolver . defer ( cb ) ; // this resolves the promise and calls the callback (asynchronously). deferred . cb ( null , 'My name is ' + name + '!' ) ; return deferred . promise ; sayMyName现在可以通过
promise-resolver-master.zip
  • promise-resolver-master
  • .gitignore
    13B
  • package.json
    1.2KB
  • .travis.yml
    230B
  • index.js
    1.4KB
  • test.js
    5KB
  • license
    1.1KB
  • readme.md
    4.9KB
  • .gitattributes
    12B
  • .editorconfig
    231B
内容介绍
# promise-resolver [![Build Status](https://travis-ci.org/jamestalmage/promise-resolver.svg?branch=master)](https://travis-ci.org/jamestalmage/promise-resolver) > Provide flexible methods that accept callbacks and return promises without requiring a Promise implementation exist ## Install ``` $ npm install --save promise-resolver ``` ## Usage ```js var promiseResolver = require('promise-resolver'); function sayMyName(name, cb) { var deferred = promiseResolver.defer(cb); // this resolves the promise and calls the callback (asynchronously). deferred.cb(null, 'My name is ' + name + '!'); return deferred.promise; ``` `sayMyName` can now be used in one of two ways: ##### Provide a callback ```js sayMyName('James', function (error, message) { console.log(message); // => 'My name is James!' }); ``` ##### Use the returned promise ```js sayMyName('Susan').then(function (message) { console.log(message); // => 'My name is Susan!' }); ``` If you do not provide a callback, then you should `catch` errors on the promise. Most promise implementations emit [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection) events. If a callback is provided, [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection) events are be suppressed (it is assumed the callback handles any errors). #### Safe Callbacks `promise-resolver` protects against callback misuse in the following ways: ```js function doStuff (cb) { var deferred = promiseResolver(cb); // prevent these typical problems: deferred.cb() // synchronous invocation deferred.cb() // multiple invocations deferred.cb() // undefined is not a function (deferred.cb is always defined, even if cb is not) } ``` #### Missing Promise Implementation `promise-resolver` allows you to create API's that provide the convenience of Promises, without demanding a bulky Promise polyfill on systems that do not already have an implementation. It looks first for `bluebird` and then a native `Promise` implementation. If the user does *not* supply a callback *and* no promise implementation is found, an error will be thrown explaining how to resolve the problem: ``` No Promise Implementation: You must use a callback function, upgrade to Node >= 0.11.13, or install bluebird ``` If it does *not* find a promise implementation, but a callback *is* found then it will still return a `deferred`, but `deferred.promise` will be `undefined`. Finally, `promiseResolver.defer(cb, Promise)` does allow you to specify an alternate Promise implementation as the second argument. ## API ### promiseResolver.defer(passThrough, Promise) * `passThrough` - a "pass through" node style callback as described above * `Promise` - an alternate Promise constructor (will use `bluebird` or native `Promise` implementation by default). The return value is a standard `defer` object with an additional `cb` property that is a node style resolver callback. ```js var deferred = promiseResolver(passThroughCallback); // rejects promise and calls passThroughCallback with same args deferred.cb(new Error('...')); // resolves promise and calls passThroughCallback with same args deferred. cb(null, 'result'); return deferred.promise; ``` * `deferred.cb` will resolve/reject the promise and call `passThroughCallback` * Ensures that `passThroughCallback` is only called once. * If `passThroughCallback` is provided, it is assumed to handle any errors, and so `unhandledRejection` errors on the promise will be suppressed. This avoids potentially confusing console warnings if users are handling errors via a callback and ignoring the returned promise. * `deferred.resolve` and `deferred.reject` are also available, and behave as expected. * `deferred.promise` will be `undefined` if no Promise implementation is found (in that case `passThroughCallback` is required). ### promiseResolver(resolve, reject, passThrough) All arguments should be functions, null, or undefined. * `resolve` - promise resolve function * `reject` - promise reject function * `passThrough` - a "pass through" node style (error first) callback. Returns a node style callback: `cb(err, result...)` Calling the callback will resolve or reject the promise (depending on the `err` argument). If it exists, the `passThrough` callback will be called with the same arguments. ```js var promiseResolver = require('promise-resolver'); return new Promise(function (resolve, reject) { var cb = promiseResolver(resolve, reject, passThroughCallback); cb(new Error('...')); cb(null, 'result'); }); ``` This behaves similar to the `defer` method, with the only exception being that `unhandledRejection` errors are *not* automatically suppressed when `passThroughCallback` is supplied. It also requires you find and invoke the Promise implementation. ## License MIT © [James Talmage](http://github.com/jamestalmage)
评论
    相关推荐
    • npm
      npm install 编译和热重装以进行开发 npm run serve 编译并最小化生产 npm run build 运行测试 npm run test 整理和修复文件 npm run lint 运行单元测试 npm run test:unit 自定义配置 请参阅。
    • npm-max:NPM软件包
      最大NPM 该函数返回最大值 npm install
    • npm-exercises
      npm-exercises
    • npmTest:测试npm
      npmTest 测试npm
    • npm-lifecycle:npm生命周期脚本运行器
      npm v7发布后,将不建议使用此模块。 请不要绝对依赖它。 npm v7中使用的生命周期脚本运行程序是 。 请继续使用该模块。 npm-lifecycle 是一个独立的库,用于执行软件包的生命周期脚本。 它是从npm本身提取的,...
    • TSProjectDemo:npm ts
      TSProjectDemo:npm ts
    • npmd-install
      npmd 安装 从 npmd-resolve 树安装节点模块。 npm install - g npmd - resolve npmd - install npmd - resolve browserify | npmd - install 另外, 一个关键部分 执照 麻省理工学院
    • npm-cli-login
      npm-cli-login 允许您不使用STDIN,STDOUT登录到NPM。 在CI构建系统等地方使用。 还可以在〜/ .npmrc文件中创建/修改条目以进行身份​​验证。 安装 npm install -g npm-cli-login 使用-g标志通过CLI使用npm-cli-...
    • npm-practice
      npm-practice
    • npm:测试
      npm 测试