-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-promise.js
54 lines (40 loc) · 1.32 KB
/
test-promise.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
'use strict';
// const _sync = require('sync-es6'); /*How to call in your code*/
const _sync = require('./sync.js');
const _request = require('request-promise');
const demoPromise = (v1, v2) => {
return new Promise( (resolve, reject) => {
console.log('\demoPromise v1', v1, 'v2', v2);
setTimeout( () => {
return resolve('Promise success:' + v1 + v2);
// return reject(new Error('Promise failed'));
}, 1e3)
});
}
const demoAsync = (v1, v2, callback) => {
setTimeout( () => {
console.log('\ndemoAsync v1', v1, 'v2', v2);
// Uncomment if you want to test error case
// if (v1 == 5 || v1 == 'c') return callback('ECRASH');
return callback(null, 'Async success: ' + v1 + v2);
}, 1e3)
}
function* demoGenerator() {
let demo = yield [null, demoPromise, 'a', 'b'];
console.log('demo', demo)
let html = yield [null, _request, 'http://www.google.com'];
console.log('google', html.length)
html = yield [null, _request, 'http://www.bing.com'];
console.log('bing', html.length)
return html;
}
let iter = _sync({generator: demoGenerator, isTolerant: true}, (err, val) => {
console.log('\nEND:', {err: err, val: typeof val});
});
// _request('http://www.google.com')
// .then(function (htmlString) {
// console.log('htmlString', htmlString.length)
// })
// .catch(function (err) {
// console.log('err', err)
// });