采用js6到js5的代码
Adopt the code from js6 to js5
我想使用此示例代码,但我需要使用 javaScript5 而不是 6,因为我们正在处理节点 0.12.7,现在无法升级。
(我用的是蓝鸟)
这是js6中的代码
checkAppPort: function(port, retriesLeft) {
return new Promise((resolve, reject) => {
this.checkPortStatus(port, host).then(resolve, error => {
setTimeout(() => {
this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
});
});
现在我已将其更改为以下内容,但出现错误
[ReferenceError: resolve is not defined]
return new Promise(resolve, reject), function () {
this._checkPortStatus(port, HOST).then(resolve, error), function () {
console.log("Waiting for App to start: checking port: " + port + " attempt: " + retriesLeft);
setTimeout(function () {
this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
}
};
},
你那里有一些奇怪的语法。就像你试图调用 new Promise(resolve, reject)
和 .then(resolve, reject)
(即使有效,也不是你想要的)。
您应该执行以下操作:
new Promise(function (resolve, reject) {
this._checkPortStatus(port, HOST).then(resolve, function () {
...
});
});
我刚刚修复了您的函数调用以接收 resolve
和 reject
作为这些函数的参数。
此外,正如@Jaromanda X 评论的那样,您最好尝试Babel 将您的代码从 ES6 转换为 ES5,这样您在转换 JS 代码时就不会再处理此类问题了。
运行 你的 ES2015 代码通过 babel yields
checkAppPort: function checkAppPort(port, retriesLeft) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.checkPortStatus(port, host).then(resolve, function (error) {
setTimeout(function () {
_this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
});
});
}
The ideal solution would be to change your ES2015 to avoid the new Promise blah blah blah
正如您使用 Bluebird 标记的那样
checkAppPort: function checkAppPort(port, retriesLeft) {
var _this = this;
return this.checkPortStatus(port, host)["catch"](function (error) {
return retriesLeft > 0 ? Promise.delay(1000, function () {
return _this.checkAppPort(port, retriesLeft - 1);
}) : Promise.reject(error);
});
}
我认为这是等效代码 (ES5) - ES6 版本是
checkAppPort: function (port, retriesLeft) {
return this.checkPortStatus(port, host)
.catch(error => retriesLeft > 0 ? Promise.delay(1000, () => this.checkAppPort(port, retriesLeft - 1)) : Promise.reject(error))
}
我认为这可能不是代码的 "tightest" 版本
to answer the comment
在 ES5 中
checkAppPort: function checkAppPortAlt(port, retries) {
var _this = this;
var checkit = function checkit(retriesLeft, retry) {
if (retry) {
console.log("Retry ...", retry);
}
return _this.checkPortStatus(port, host)["catch"](function (error) {
return retriesLeft > 0 ? Promise.delay(1000, function () {
return checkit(retriesLeft - 1, retry + 1);
}) : Promise.reject(error);
});
};
return checkit(retries, 0);
}
在 ES2016 中是
checkAppPortAlt: function (port, retries) {
var checkit = (retriesLeft, retry) => {
if(retry) {
console.log("Retry ...", retry);
}
return this.checkPortStatus(port, host)
.catch(error => retriesLeft > 0 ? Promise.delay(1000, () => checkit(retriesLeft - 1, retry + 1)) : Promise.reject(error))
};
return checkit(retries, 0);
}
未测试,但可能是正确的 :D - 我担心 this
在 ES2015 代码中的正确性 - 不过,babel 似乎将它翻译成应该在 ES5
中工作的东西
我想使用此示例代码,但我需要使用 javaScript5 而不是 6,因为我们正在处理节点 0.12.7,现在无法升级。 (我用的是蓝鸟)
这是js6中的代码
checkAppPort: function(port, retriesLeft) {
return new Promise((resolve, reject) => {
this.checkPortStatus(port, host).then(resolve, error => {
setTimeout(() => {
this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
});
});
现在我已将其更改为以下内容,但出现错误
[ReferenceError: resolve is not defined]
return new Promise(resolve, reject), function () {
this._checkPortStatus(port, HOST).then(resolve, error), function () {
console.log("Waiting for App to start: checking port: " + port + " attempt: " + retriesLeft);
setTimeout(function () {
this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
}
};
},
你那里有一些奇怪的语法。就像你试图调用 new Promise(resolve, reject)
和 .then(resolve, reject)
(即使有效,也不是你想要的)。
您应该执行以下操作:
new Promise(function (resolve, reject) {
this._checkPortStatus(port, HOST).then(resolve, function () {
...
});
});
我刚刚修复了您的函数调用以接收 resolve
和 reject
作为这些函数的参数。
此外,正如@Jaromanda X 评论的那样,您最好尝试Babel 将您的代码从 ES6 转换为 ES5,这样您在转换 JS 代码时就不会再处理此类问题了。
运行 你的 ES2015 代码通过 babel yields
checkAppPort: function checkAppPort(port, retriesLeft) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.checkPortStatus(port, host).then(resolve, function (error) {
setTimeout(function () {
_this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
}, 1000);
});
});
}
The ideal solution would be to change your ES2015 to avoid the new Promise blah blah blah
正如您使用 Bluebird 标记的那样
checkAppPort: function checkAppPort(port, retriesLeft) {
var _this = this;
return this.checkPortStatus(port, host)["catch"](function (error) {
return retriesLeft > 0 ? Promise.delay(1000, function () {
return _this.checkAppPort(port, retriesLeft - 1);
}) : Promise.reject(error);
});
}
我认为这是等效代码 (ES5) - ES6 版本是
checkAppPort: function (port, retriesLeft) {
return this.checkPortStatus(port, host)
.catch(error => retriesLeft > 0 ? Promise.delay(1000, () => this.checkAppPort(port, retriesLeft - 1)) : Promise.reject(error))
}
我认为这可能不是代码的 "tightest" 版本
to answer the comment
在 ES5 中
checkAppPort: function checkAppPortAlt(port, retries) {
var _this = this;
var checkit = function checkit(retriesLeft, retry) {
if (retry) {
console.log("Retry ...", retry);
}
return _this.checkPortStatus(port, host)["catch"](function (error) {
return retriesLeft > 0 ? Promise.delay(1000, function () {
return checkit(retriesLeft - 1, retry + 1);
}) : Promise.reject(error);
});
};
return checkit(retries, 0);
}
在 ES2016 中是
checkAppPortAlt: function (port, retries) {
var checkit = (retriesLeft, retry) => {
if(retry) {
console.log("Retry ...", retry);
}
return this.checkPortStatus(port, host)
.catch(error => retriesLeft > 0 ? Promise.delay(1000, () => checkit(retriesLeft - 1, retry + 1)) : Promise.reject(error))
};
return checkit(retries, 0);
}
未测试,但可能是正确的 :D - 我担心 this
在 ES2015 代码中的正确性 - 不过,babel 似乎将它翻译成应该在 ES5