Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/josudoey/promise4solo
provide a wrap function for promise solo
https://github.com/josudoey/promise4solo
Last synced: about 1 month ago
JSON representation
provide a wrap function for promise solo
- Host: GitHub
- URL: https://github.com/josudoey/promise4solo
- Owner: josudoey
- License: isc
- Created: 2017-02-10T13:22:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-17T15:13:33.000Z (almost 8 years ago)
- Last Synced: 2024-11-29T22:47:09.626Z (about 1 month ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# promise4solo
[![NPM](https://nodei.co/npm/promise4solo.svg?downloads=true&downloadRank=true)](https://nodei.co/npm/promise4solo/)
[![Build Status](https://travis-ci.org/josudoey/promise4solo.svg?branch=master)](https://travis-ci.org/josudoey/promise4solo)
[![Test Coverage](https://codeclimate.com/github/josudoey/promise4solo/badges/coverage.svg)](https://codeclimate.com/github/josudoey/promise4solo/coverage)
[![Code Climate](https://codeclimate.com/github/josudoey/promise4solo/badges/gpa.svg)](https://codeclimate.com/github/josudoey/promise4solo)
[![Issue Count](https://codeclimate.com/github/josudoey/promise4solo/badges/issue_count.svg)](https://codeclimate.com/github/josudoey/promise4solo)## Installation
```bash
$ npm install --save promise4solo
```## Example
```js
var solo = require('promise4solo')();
var now = function () {
this.start = this.start || Date.now();
return Date.now() - this.start;
};var singAsync = function (ms) {
var self = this;
var ts = now();
if (!ms) {
throw new Error(ts + self.name + ' oops');
}
console.log(ts + self.name + ' begin sing ' + ms);
return new Promise(function (resolve) {
setTimeout(function () {
var end = now();
console.log(end + self.name + ' end sing ' + ms);
resolve(end);
}, ms);
});
};var who = {
name: ' joey'
};var singSolo = solo(singAsync, who);
var saySolo = solo(function (msg) {
console.log(now() + this.name + ' say ' + msg);
}, who);singSolo(1000);
saySolo('hi');
singSolo().catch(function (err) {
console.log(err.message);
});
singSolo(100).then(function (end) {
console.log(end + ' done');
});
console.log(now() + ' start');// Output:
// 0 start
// 3 joey begin sing 1000
// 1011 joey end sing 1000
// 1011 joey say hi
// 1011 joey oops
// 1012 joey begin sing 100
// 1118 joey end sing 100
// 1118 done
```## API
Return a solo wrap function, and the wrap function will return promise and guarantee one async function be run for order async flow.
### solo(func[, thisArg])
```js
var funcAsync = function(val){
return new Promise(function(resolve){
//...
resolve(val);
});
}var funcSolo = solo(funcAsync);
funcSolo(true).then(function (val) {});
```