1 Star 0 Fork 0

martian2049/node

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
async.js 11.72 KB
一键复制 编辑 原始数据 按行查看 历史
martian2049 提交于 2018-07-04 14:55 . session
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
// // ---------------------------------------
// function* idGen(){
// for(let i=0;i<3;i++){
// console.log("--->",i);
// yield i
// }
// }
// https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9
// var gen = idGen();
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// // ---------------------------------------
// function* idGen() {
// for (let i = 0; i < 3; i++) {
// console.log("--->", i);
// yield i;
// }
// }
// var gen = idGen();
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// // ---------------------------------------
// function* idGen() {
// for (let i = 0; i < 3; i++) {
// console.log("--->", i);
// }
// }
// var gen = idGen();
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// // ---------------------------------------
// function* foo(x) {
// while (true) {
// x = x * 2;
// yield x;
// }
// }
// var g = foo(2);
// console.log(g.next());
// console.log(g.next());
// console.log(g.next());
// // // ---------------------------------------
// function* bar(x) {
// x++;
// var y = yield x;
// yield y/2;
// }
// var g = bar(1);
// console.log(g.next()); // -> 2
// console.log(g.next(8)); // -> 2
// // g.send(8); // -> 4
// // ---------------
// function* foo(x) {
// let a = 'aaa'
// console.log(x);
// setTimeout(function() {
// // yield a
// console.log(x,a)
// }, 0);
// yield a
// // while (true) {
// // x = x * 2;
// // yield x;
// // }
// }
// var g = foo(2);
// console.log(g.next());
// // console.log(g.next());
// // console.log(g.next());
// // ---------------
// function* foo(x) {
// let a = 'aaa';
// (function(){
// // yield 'a'
// console.log('--')
// })();
// yield a
// // while (true) {
// // x = x * 2;
// // yield x;
// // }
// }
// var g = foo(2);
// console.log(g.next());
// // console.log(g.next());
// // console.log(g.next());
// function *g(){
// console.log('a');
// yield "yoyo";
// console.log('b');
// yield "haha";
// }
// var g1 = g();
// console.log(g1.next());
// console.log(g1.next());
// console.log(g1.next());
// console.log(g1.next());
// var g2 = g();
// for(let val of g2){
// console.log(val);
// }
// var g3 = g();
// console.log('g3 -->',[...g3])
// (function() {
// for (let argument of arguments) {
// console.log(argument);
// }
// })(1, 2, 3);
// function dog(sound){
// console.log(arguments);
// }
// dog('wowow')
// var myGen = function*(){
// var one = yield 1;
// var two = yield 2;
// var three = yield 3;
// console.log(one, two, three);
// }
// var gen = myGen();
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// var myGen = function*() {
// var closure="this is a closed variable";
// var one = yield 1;
// console.log('one->',one);
// var two = yield 2;
// console.log('two->',two);
// var three = yield 3;
// console.log('three->',three);
// console.log("-->",one, two, three);
// };
// var gen = myGen(); //get the generator ready to run
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log("---------------------------------")
// var gen2 = myGen(); //get the generator ready to run
// console.log(gen2.next(9));
// console.log(gen2.next(8));
// // console.log(gen2.next(7));
// // console.log(gen2.next(6));
// // console.log(gen2.next(5));
// var myGen = function*() {
// var closure="x-x-x-x-x- this is a closed variable";
// var one = yield 1;
// console.log('one->',one);
// var two = yield 2;
// console.log('two->',two);
// var three = yield 3;
// console.log(closure)
// console.log('three->',three);
// console.log("-->",one, two, three);
// };
// var gen = myGen(); //get the generator ready to run
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log(gen.next());
// console.log("---------------------------------")
// var gen2 = myGen(); //get the generator ready to run
// console.log(gen2.next(9));
// console.log(gen2.next(8));
// // console.log(gen2.next(7));
// // console.log(gen2.next(6));
// // console.log(gen2.next(5));
// // ---------------- anonymous gen... cool
// var gen = (function *(){
// yield 1;
// yield 2;
// yield 3;
// })();
// for (let o of gen) {
// console.log(o);
// }
// //for ... in // enumerable: key(index) based
// //for ... of // interable: return stuff
// var obj={
// b:'bbbb',
// a:'aaa',
// }
// for(let prop in obj){
// console.log(prop)
// }
// // for(let prop of obj){
// // console.log(prop)
// // }
// var arr=['aa','bb','cc'];
// for(let prop of arr){
// console.log(prop)
// }
// for(let prop in arr){
// console.log(prop)
// }
// //--- turn this into asycn series???
// function* foo(){
// yield 1;
// yield 2;
// yield 3;
// };
// for (let o of foo()) {
// console.log(o);
// // break; // closes iterator, triggers return
// }
// //----------------------------------
// function* func1() {
// yield 42;
// }
// function* func2() {
// yield* func1();
// }
// const iterator = func2();
// console.log(iterator.next().value);
// // expected output: 42
// function func3(){ return func1() }
// const iter = func3();
// console.log(iter.next());
// function* func4(){ yield func1()};
// const iter4 = func4();
// console.log(iter4.next());
// function* dogs(){
// yield 'Cho'
// yield 'Boddy'
// yield "Wiley"
// yield "Johnson"
// yield "Moew"
// yield "Witty"
// yield "Dessy"
// yield "muu"
// }
// console.log([...dogs()])
// var a = 1;
// var b = 3;
// var c = 10;
// [a,c, b] = [b, a,c];
// console.log(a);
// console.log(b);
// console.log(c);
// ({a, b} = {a: 1, b: 2})
// console.log(a,b)
// // {a, b} = {a: 1, b: 2}
// function userId({id}) {
// return id;
// }
// function whois({displayName, fullName: {firstName: name}}) {
// console.log(displayName + ' is ' + name);
// }
// function simple({displayName,fullName}){
// console.log(displayName);
// console.log(fullName);
// }
// var user = {
// id: 42,
// displayName: 'jdoe',
// fullName: {
// firstName: 'John',
// lastName: 'Doe'
// }
// };
// console.log('userId: ' + userId(user)); // "userId: 42"
// whois(user); // "jdoe is John"
// simple(user);
// // //------------ asynch w/ fetch
// const fetch = require("node-fetch");
// const co = require("co");
// process.on('unhandledRejection', (reason, p) => {
// console.log('-----------bug log splitter----------')
// console.log('Unhandled Rejection at: ', p);
// console.log('Unhandled Rejection reason:', reason)
// // application specific logging, throwing an error, or other logic here
// console.log('-----------bug log splitter----------')
// });
// fetch('http://localhost:3009/candies')
// .then(res => {
// console.log(res);
// console.log(res.status);
// console.log(res.statusText);
// // console.log(res.headers.raw());
// // console.log(res.headers.get('content-type'));
// });
// fetch('http://localhost:3009/candies')
// .then(res=>res.json())
// .then(json=>console.log(json));
// var myGen = function*(){
// yield fetch('http://localhost:3009/candies').then(res=>res.json());
// yield fetch('http://localhost:3009/bottles').then(res=>res.json());
// };
// var gen = myGen();
// console.log(gen.next().value.then(json=>console.log(json)));
// console.log(gen.next().value.then(json=>console.log(json)));
// var final = [...gen].map((stuff)=>{
// var shit = "";
// stuff.then(json=>shit=json)
// return shit
// });
// console.log(final)
// var myGen = function*(){
// yield $.get('http://localhost:3009/candies');
// // yield fetch('http://localhost:3009/bottles').then(res=>res.json());
// };
// var gen = myGen();
// console.log(gen.next());
// // console.log(gen.next().value.then(json=>console.log(json)));
// //------------ asynch w/ fetch
const fetch = require("node-fetch");
const co = require("co");
process.on('unhandledRejection', (reason, p) => {
console.log('-----------bug log splitter----------')
console.log('Unhandled Rejection at: ', p);
console.log('Unhandled Rejection reason:', reason)
// application specific logging, throwing an error, or other logic here
console.log('-----------bug log splitter----------')
});
// co(function*(){
// const url = "http://localhost:3009/candies";
// const res1 = yield fetch(url);
// // console.log(res.json())
// const candies = yield res1.json();
// console.log(candies);
// const url2 = "http://localhost:3009/bottles";
// const res2 = yield fetch(url2);
// // console.log(res.json())
// const bottles = yield res2.json();
// console.log(bottles);
// });
// -------------- good ----------------
async function candyOrBottle() {
var result;
try {
result = await fetch("http://localhost:3009/candies").then(res=>res.json());
} catch(e) {
// console.log(e)
result = await fetch("http://localhost:3009/bottles").then(res=>res.json());
}
return result
}
candyOrBottle()
.then(res=>console.log("result is---> ",res))
.catch(err=>console.log(err));
console.log('----------');
// try {
// myroutine(); // may throw three types of exceptions
// } catch (e if e instanceof TypeError) {
// // statements to handle TypeError exceptions
// } catch (e if e instanceof RangeError) {
// // statements to handle RangeError exceptions
// } catch (e if e instanceof EvalError) {
// // statements to handle EvalError exceptions
// } catch (e) {
// // statements to handle any unspecified exceptions
// logMyErrors(e); // pass exception object to error handler
// }
async function leftover(){
const candies = await fetch("http://localhost:3009/candies").then(res=>res.json());
const bottles = await fetch("http://localhost:3009/bottles").then(res=>res.json());
return [candies,bottles]
};
leftover()
.then(res=>console.log(res))
.catch(err=>console.log(err));
function logval(val){
console.log("---->",val);
};
console.log("-x-x-x-x-x-x");
co(function*() {
const candies = yield fetch("http://localhost:3009/candies").then(res =>
res.json()
);
// console.log(candies);
const bottles = yield fetch("http://localhost:3009/bottles").then(res =>
res.json()
);
// console.log(bottles);
return [candies,bottles]
})
.then(logval)
.catch(logval);
console.log("-x-x-x-x-x-x");
co(function*() {
var res = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
console.log(res); // => [1, 2, 3]
}).catch(logval);
co(function*() {
var res = yield {
1: Promise.resolve(1),
2: Promise.resolve(2)
};
console.log(res); // => { 1: 1, 2: 2 }
}).catch(logval);
// ------------------------
const arr = [{ key: 1 }, { key: 2 }, { key: 3 }];
const results = arr.map(async obj => {
return obj.key;
});
Promise.all(results).then(completed => console.log(completed));
// https://gist.github.com/indiesquidge/5960274889e17102b5130e8bd2ce9002
// https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need-promises-9b259854c161
async function makePizza(sauceType = 'red') {
let [ dough, sauce ] =
await Promise.all([ makeDough(), makeSauce(sauceType) ]);
let cheese = await grateCheese(sauce.determineCheese());
dough.add(sauce);
dough.add(cheese);
return dough;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/martian2049/node.git
git@gitee.com:martian2049/node.git
martian2049
node
node
master

搜索帮助