我有以下内容:
resolutionTestBuilder.buildTests().then(function(tests) {
var s = tests; // checking result
});
buildTests
看起来像这样:
resolutionTestBuilder.buildTests = function () {
Promise.all([createAllResolutions(), deviceScanner.scanDevices()])
.spread(function (resolutions, videoDevices) {
var tests = [];
resolutions.forEach(function (targetResolution) {
videoDevices.forEach(function (videoDevice) {
tests.push({ device: videoDevice, resolution: targetResolution });
});
});
return tests;
});
}
createAllResolutions
看起来像这样:
var createAllResolutions = function () {
for (let y = maxHeight; y >= minHeight; y--) {
resolutions.push(
{x:x,y:y}
);
}
return resolutions;
}
最后,scanDevices
看起来像这样:
deviceScanner.scanDevices = function() {
navigator.mediaDevices.enumerateDevices().then(function(availableDevices) {
var videoDevices = [];
for (var i = 0; i < availableDevices.length; ++i) {
if (availableDevices[i].kind === 'videoinput') {
videoDevices.push({ label: availableDevices[i].label || 'camera ' + (videoDevices.length + 1), id: availableDevices[i].deviceId });
}
}
return videoDevices;
});
}
我看到的行为是.spread
参数部分完成 - 我得到resolutions
但不是videoDevices
。这是我对承诺(和蓝鸟)的第一次尝试,所以我可能在这里遗漏了一些非常基本的东西 - 我做错了什么?
答案 0 :(得分:2)
return
回调的{p> then
是不够的,您需要return
then()
来自该函数的调用的承诺!
resolutionTestBuilder.buildTests = function () {
return Promise.all([createAllResolutions(), deviceScanner.scanDevices()])
// ^^^^^^
.spread(function (resolutions, videoDevices) {
…
});
};
deviceScanner.scanDevices = function() {
return navigator.mediaDevices.enumerateDevices().then(function(availableDevices) {
// ^^^^^^
…
});
};