-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (101 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*!
* letta <https://github.com/hybridables/letta>
*
* Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
/**
* > Control flow for now and then.
*
* **Example**
*
* ```js
* const letta = require('letta')
*
* letta((foo, bar, baz) => {
* console.log(foo, bar, baz) // => 'foo bar baz'
* return foo
* }, 'foo', 'bar', 'baz')
* .then(console.log) // => 'foo'
* ```
*
* @name letta
* @param {Function} `<fn>` Regular function (including arrow function) or generator function.
* @param {Mixed} `[...args]` Any number of any type of arguments, they are passed to `fn`.
* @return {Promise} Always native Promise if supported on enviroment.
* @api public
*/
var letta = module.exports = function letta (fn, args) {
var self = this
var Promize = utils.nativeOrAnother(letta.Promise)
args = utils.sliced(arguments, 1)
var promise = new Promize(function (resolve, reject) {
if (utils.isGenFn(fn)) {
utils.co.apply(self, [fn].concat(args)).then(resolve, reject)
return
}
utils.relike.Promise = letta.Promise
utils.relike.apply(self, [fn].concat(args)).then(resolve, reject)
})
return utils.normalizePromise(promise, Promize)
}
/**
* > Returns a function that will wrap the given `fn`.
* Instead of taking a callback, the returned function will
* return a promise whose fate is decided by the callback
* behavior of the given `fn` node function. The node function
* should conform to node.js convention of accepting a callback
* as last argument and calling that callback with error as the
* first argument and success value on the second argument.
* – [Bluebird Docs on `.promisify`](http://bluebirdjs.com/docs/api/promise.promisify.html)
*
* **Example**
*
* ```js
* const fs = require('fs')
* const letta = require('letta')
* const readFile = letta.promisify(fs.readFile)
*
* readFile('package.json', 'utf8')
* .then(JSON.parse)
* .then(value => {
* console.log(value.name) // => 'letta'
* })
* .catch(SyntaxError, err => {
* console.error('File had syntax error', err)
* })
* // Catch any other error
* .catch(err => {
* console.error(err.stack)
* })
*
* // or promisify generator function
* const promise = letta(function * () {
* let result = yield Promise.resolve(123)
* return result
* })
*
* promise.then(value => {
* console.log(value) // => 123
* }, err => {
* console.error(err.stack)
* })
* ```
*
* @name .promisify
* @param {Function} `<fn>` Regular function (including arrow function) or generator function.
* @param {Function} `[Promize]` Promise constructor to be used on enviroment where no support for native.
* @return {Function} Promisified function, which always return a Promise when called.
* @api public
*/
letta.promisify = letta.wrap = function lettaPromisify (fn, Promize) {
var self = this
function promisified () {
letta.Promise = Promize || lettaPromisify.Promise || promisified.Promise
return letta.apply(this || self, [fn].concat(utils.sliced(arguments)))
}
promisified.__generatorFunction__ = fn
return promisified
}