-
Notifications
You must be signed in to change notification settings - Fork 1
/
generators.js
50 lines (41 loc) · 1.01 KB
/
generators.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
/*!
* always-promise <https://github.com/hybridables/always-promise>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var fs = require('fs')
var alwaysPromise = require('../index')
var simpleGet = require('simple-get')
/**
* readFile
*/
function read (fp) {
return function (done) {
fs.readFile(fp, 'utf8', done)
}
}
alwaysPromise(function * (filepath) {
var data = yield read(filepath)
return JSON.parse(data)
})('package.json').then(function (json) {
console.log(json.name) // => 'always-promise'
}, console.error)
/**
* http request
*/
function get (url) {
return function (done) {
simpleGet.concat(url, done)
}
}
alwaysPromise(function * (url) {
var res = yield get(url)
var buf = res[0]
// var httpResponse = res[1]
return buf.toString()
})('http://www.tunnckocore.tk').then(function (res) {
console.log(res.indexOf('<title>') !== -1) // => true
}, console.error)