你能帮助我满足 NodeJS 中的 promises aplus spec 2.2.4 吗?
Can you assist me in satisfying promises aplus spec 2.2.4 in NodeJS?
我正在为 nodeJS 编写一个 HTTP Promises 包。
我需要它符合 promises aplus 的要求。
我正在使用 promises-aplus-tests - https://www.npmjs.com/package/promises-aplus-tests
我创建了一个适配器如下..
var adapter = {
deferred: function() {
var d = {}
d.promise = new HTTPromise(new GETOptions('/path', DEFAULT_OPTIONS));
d.resolve = executorResponseFunc;
d.reject = executorRejectFunc;
return d;
}
}
promisesAplusTests(adapter, function (err) {
done(err);
});
我的测试在 https://promisesaplus.com/#point-34 上失败了...
在 https://promisesaplus.com/#point-67
上获得进一步帮助
2.2.4 onFulfilled or onRejected must not be called until the execution
context stack contains only platform code
3.1 Here “platform code” means engine, environment, and promise
implementation code. In practice, this requirement ensures that
onFulfilled and onRejected execute asynchronously, after the event
loop turn in which then is called, and with a fresh stack. This can be
implemented with either a “macro-task” mechanism such as setTimeout or
setImmediate, or with a “micro-task” mechanism such as
MutationObserver or process.nextTick. Since the promise implementation
is considered platform code, it may itself contain a task-scheduling
queue or “trampoline” in which the handlers are called.
但我无法让它通过这个 promises aplus 测试。我正在使用记录在 ...
的沼泽标准 JavaScript Promises 库(它自己通过了测试)
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
new Promise(执行者);
new Promise(function(resolve, reject) { ... });
在我试过的executor中...
if (response.statusCode === 200) {
process.nextTick(() => {
resolve(finalResponse);
});
} else {
process.nextTick(() => {
reject(finalResponse);
});
}
还有...
process.nextTick(() => {
if (response.statusCode === 200) {
resolve(finalResponse);
} else {
reject(finalResponse);
}
});
网上好像说的很多,但是关于如何遵守的代码示例并不多。谁能提供代码示例,说明如何满足此测试以及如何使用 nextTick 或其他合适的解决方案?
值得一提的是,我在执行程序函数内的 HTTP 错误处理程序中有另一个拒绝调用;
非常感谢
我想向 Bergi 道歉,我想我已经找到了我愚蠢的答案,但我希望确认我的想法现在是正确的...
我最初的困惑来自...
https://www.npmjs.com/package/request-promise
我正在编写一个类似但更强大的 npm 工具。
他们有 "then" 徽章,显然我想要它!
我想我需要通过你们的测试(他们已经做了某种尝试)。
所以我试图通过你的 promises-aplus-tests
与我的图书馆一起使用,它大量使用承诺但不是承诺实现。
我明白了……
https://www.npmjs.com/package/promises-aplus-tests
说
This suite tests compliance of a promise implementation with the
Promises/A+ specification.
所以我现在意识到我的包裹不需要通过你的测试,我也没有我希望的那么聪明:)
但是,我是 运行 一个测试,以确保我们使用的 Promise 库在构建过程中通过您的测试。如果不是,构建将失败。
我的代码现在是...
class HTTPromise {
constructor(options) {
... blah blah blah
let executor = THE_REAL_EXECUTOR;
return AppPromise(executor);
}
}
class HTTPromiseCompliance {
constructor(executor) {
return AppPromise(executor);
}
}
AppPromise 看起来像...
module.exports = executor => {
return new Promise(executor);
}
最后是测试套件...
describe('HTTPromises', function () {
it('should use a Promises/A+ compliant Promise library', function (done) {
this.timeout(0);
var adapter = {
deferred: function() {
var d = {}
var executor = function(resolve, reject) {
d.resolve = resolve
d.reject = reject
};
d.promise = new HTTPromiseCompliance(executor);
return d
}
}
promisesAplusTests(adapter, function (err) {
if (err != null) {
console.log('promises Aplus Tests failed with ' + err.failures + ' failures');
} else {
console.log('promises Aplus Tests passed');
}
done(err);
});
});
});
在构建过程中为此触发了一个 grunt 任务。
如果有人对'my new thinking'有任何想法,我将不胜感激。
谢谢
我正在为 nodeJS 编写一个 HTTP Promises 包。 我需要它符合 promises aplus 的要求。 我正在使用 promises-aplus-tests - https://www.npmjs.com/package/promises-aplus-tests
我创建了一个适配器如下..
var adapter = {
deferred: function() {
var d = {}
d.promise = new HTTPromise(new GETOptions('/path', DEFAULT_OPTIONS));
d.resolve = executorResponseFunc;
d.reject = executorRejectFunc;
return d;
}
}
promisesAplusTests(adapter, function (err) {
done(err);
});
我的测试在 https://promisesaplus.com/#point-34 上失败了... 在 https://promisesaplus.com/#point-67
上获得进一步帮助2.2.4 onFulfilled or onRejected must not be called until the execution context stack contains only platform code
3.1 Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called.
但我无法让它通过这个 promises aplus 测试。我正在使用记录在 ...
的沼泽标准 JavaScript Promises 库(它自己通过了测试)https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
new Promise(执行者);
new Promise(function(resolve, reject) { ... });
在我试过的executor中...
if (response.statusCode === 200) {
process.nextTick(() => {
resolve(finalResponse);
});
} else {
process.nextTick(() => {
reject(finalResponse);
});
}
还有...
process.nextTick(() => {
if (response.statusCode === 200) {
resolve(finalResponse);
} else {
reject(finalResponse);
}
});
网上好像说的很多,但是关于如何遵守的代码示例并不多。谁能提供代码示例,说明如何满足此测试以及如何使用 nextTick 或其他合适的解决方案?
值得一提的是,我在执行程序函数内的 HTTP 错误处理程序中有另一个拒绝调用;
非常感谢
我想向 Bergi 道歉,我想我已经找到了我愚蠢的答案,但我希望确认我的想法现在是正确的...
我最初的困惑来自... https://www.npmjs.com/package/request-promise
我正在编写一个类似但更强大的 npm 工具。 他们有 "then" 徽章,显然我想要它! 我想我需要通过你们的测试(他们已经做了某种尝试)。 所以我试图通过你的 promises-aplus-tests 与我的图书馆一起使用,它大量使用承诺但不是承诺实现。
我明白了…… https://www.npmjs.com/package/promises-aplus-tests
说
This suite tests compliance of a promise implementation with the Promises/A+ specification.
所以我现在意识到我的包裹不需要通过你的测试,我也没有我希望的那么聪明:)
但是,我是 运行 一个测试,以确保我们使用的 Promise 库在构建过程中通过您的测试。如果不是,构建将失败。
我的代码现在是...
class HTTPromise {
constructor(options) {
... blah blah blah
let executor = THE_REAL_EXECUTOR;
return AppPromise(executor);
}
}
class HTTPromiseCompliance {
constructor(executor) {
return AppPromise(executor);
}
}
AppPromise 看起来像...
module.exports = executor => {
return new Promise(executor);
}
最后是测试套件...
describe('HTTPromises', function () {
it('should use a Promises/A+ compliant Promise library', function (done) {
this.timeout(0);
var adapter = {
deferred: function() {
var d = {}
var executor = function(resolve, reject) {
d.resolve = resolve
d.reject = reject
};
d.promise = new HTTPromiseCompliance(executor);
return d
}
}
promisesAplusTests(adapter, function (err) {
if (err != null) {
console.log('promises Aplus Tests failed with ' + err.failures + ' failures');
} else {
console.log('promises Aplus Tests passed');
}
done(err);
});
});
});
在构建过程中为此触发了一个 grunt 任务。
如果有人对'my new thinking'有任何想法,我将不胜感激。
谢谢