Ai
1 Star 13 Fork 10

Javen/Brickengine_Guide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ES6.js 4.24 KB
一键复制 编辑 原始数据 按行查看 历史
Javen205 提交于 2018-11-30 18:07 +08:00 . 添加ES6测试
/**
* @author Javen
* @copyright 2018-11-30 18:01:03 javendev@126.com
* @description QQ玩一玩中使用ES6
* 步骤1 添加 https://google.github.io/traceur-compiler/bin/traceur.js 并设置为插件
* 步骤2 如果要使用async 需要添加 https://raw.githubusercontent.com/caolan/async/master/dist/async.min.js 并设置为插件
*
* ES6 Promise 用法讲解
* https://www.cnblogs.com/whybxy/p/7645578.html
*/
let temps = ['测试ES6', 'Java', 'Node.js', 'Python', 'IJPay'];
cc.Class({
extends: cc.Component,
properties: {
},
// onLoad () {},
start() {},
toBack() {
cc.director.loadScene("Scene");
},
testAsync() {
cc.log("开始测试testAsync");
async.each(temps, function (item) {
cc.log("item:" + item + " " + new Date().getTime() + " TODO...");
//耗时操作
setTimeout(function () {
cc.log('执行完成:' + item);
}, 2000);
}.bind(this));
cc.log("结束测试testAsync");
cc.log("开始测试testAsync2");
async.eachLimit(temps, 2, function (item) {
cc.log("item:" + item + " " + new Date().getTime());
}.bind(this));
cc.log("结束测试testAsync2");
},
testPromise() {
cc.log("testPromise...");
this.runAsync()
.then(function (data) {
cc.log("回调:" + data);
cc.log(xxx); //此处的xxx未定义
})
.catch(function (reason) {
cc.log(reason);
});
cc.log("testPromise end...");
},
testPromise2() {
this.runAsync1()
.then(function (data) {
cc.log(data);
return this.runAsync2();
}.bind(this))
.then(function (data) {
cc.log(data);
return this.runAsync3();
}.bind(this))
.then(function (data) {
cc.log(data);
return "直接放回数据";
})
.then(function (data) {
cc.log(data);
});
},
testPromiseAll() {
Promise
.all([this.runAsync1(), this.runAsync2(), this.runAsync3()])
.then(function (results) {
cc.log(results);
});
},
testPromiseCatch() {
this.getNumber()
.then(function (data) {
cc.log('resolved:' + data);
})
.catch(function (reason) {
cc.log('rejected:' + reason);
});
},
runAsync() {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
setTimeout(function () {
cc.log('执行完成');
resolve('resolve 随便什么数据');
}, 2000);
});
return p;
},
runAsync1() {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
setTimeout(function () {
console.log('异步任务1执行完成');
resolve('随便什么数据1');
}, 1000);
});
return p;
},
runAsync2() {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
setTimeout(function () {
console.log('异步任务2执行完成');
resolve('随便什么数据2');
}, 2000);
});
return p;
},
runAsync3() {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
setTimeout(function () {
console.log('异步任务3执行完成');
resolve('随便什么数据3');
}, 2000);
});
return p;
},
getNumber() {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
setTimeout(function () {
var num = Math.ceil(Math.random() * 10); //生成1-10的随机数
if (num <= 5) {
resolve(num);
} else {
reject('数字太大了');
}
}, 2000);
});
return p;
},
// update (dt) {},
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/javen205/Brickengine_Guide.git
git@gitee.com:javen205/Brickengine_Guide.git
javen205
Brickengine_Guide
Brickengine_Guide
master

搜索帮助