├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 WEILAI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Promise + 2 | Promise with callback in each step. 3 | 4 | ### Install 5 | ``` 6 | npm i tvrcgo/promise 7 | ``` 8 | 9 | ### Content 10 | - `promise()` 11 | - `then()` 12 | - `catch()` 13 | 14 | ### Usage 15 | ```js 16 | promise(function(done, reject){ 17 | console.log('init'); 18 | done(3); 19 | 20 | }).then(function(result, done, reject){ 21 | console.log('next 1', result); // result=3 22 | done(2); 23 | 24 | }).then(function(result, done, reject){ 25 | console.log('next 2', result); // result=2 26 | 27 | }).catch(function(err){ 28 | console.error(err); 29 | 30 | }) 31 | ``` 32 | 33 | ### License 34 | MIT 35 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * promise 5 | * @param {function} fn 6 | */ 7 | function promise(fn){ 8 | if (!(this instanceof promise)) { 9 | return new promise(fn); 10 | } 11 | this.promise = new Promise(function(resolve, reject){ 12 | fn && fn(resolve, reject); 13 | }); 14 | }; 15 | 16 | /** 17 | * then 18 | * @param {Function} fn 19 | * @return {object} [promise] 20 | */ 21 | promise.prototype.then = function(fn){ 22 | return promise(function(resolve, reject){ 23 | this.promise.then(function(result){ 24 | fn && fn(result, resolve, reject); 25 | }).catch(function(err) { 26 | reject(err); 27 | }); 28 | }.bind(this)); 29 | } 30 | 31 | /** 32 | * catch 33 | * @param {Function} fn [description] 34 | * @return {object} [promise] 35 | */ 36 | promise.prototype.catch = function(fn) { 37 | this.promise.catch(fn); 38 | return this; 39 | } 40 | 41 | module.exports = promise; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise", 3 | "version": "1.0.0", 4 | "description": "promise with callback in each step.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "cd test && mocha test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/tvrcgo/promise.git" 12 | }, 13 | "keywords": [ 14 | "flow" 15 | ], 16 | "author": "tvrcgo", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/tvrcgo/promise/issues" 20 | }, 21 | "homepage": "https://github.com/tvrcgo/promise#readme" 22 | } 23 | --------------------------------------------------------------------------------