我的目标: 我正在尝试将源文件混编成一个输出文件,以包含在Web应用程序中。
我的问题:
我在源代码中包含了一个自定义Promise
方法。当babel兑现承诺时,它将尝试从<polyfilledPromise>.default.customMethod
调用此方法。我尝试将自定义方法添加到Promise原型中,但是babel似乎根本不在乎源代码中的Promise
。
我的问题: 有没有办法在babel已实现polyfilled的承诺中包含自定义函数?
我正在使用的cli命令:
node_modules/.bin/browserify lib/index.js --s Verifier -o verifier.js -t [ babelify --presets [ babel-preset-latest] --plugins [ transform-async-to-generator transform-runtime ] ]
我的部门:
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"bigi": "^1.4.2",
"bitcoinjs-lib": "^2.3.0",
"bluebird": "^3.5.1",
"bs58": "^3.0.0",
"buffer": "^5.0.1",
"chai": "^3.5.0",
"debug": "^3.0.1",
"dev": "^0.1.3",
"ecurve": "^1.0.4",
"jsonld": "^0.4.11",
"mocha": "^3.5.3",
"sha256": "^0.2.0",
"string.prototype.startswith": "^0.2.0",
"tsify": "^3.0.3",
"verror": "^1.9.0",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"babel-cli": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"uglify-js": "^3.0.8"
}
自定义的Promise方法:
Promise.properRace = function (promises, count, results = []) {
// Source: https://www.jcore.com/2016/12/18/promise-me-you-wont-use-promise-race/
promises = Array.from(promises);
if (promises.length < count) {
return Promise.reject(new VerifierError("Could not confirm the transaction"));
}
let indexPromises = promises.map((p, index) => p.then(() => index).catch((err) => {
log(err);
throw index;
}));
return Promise.race(indexPromises).then(index => {
let p = promises.splice(index, 1)[0];
p.then(e => results.push(e));
if (count === 1) {
return results;
}
return Promise.properRace(promises, count - 1, results);
}).catch(index => {
promises.splice(index, 1);
return Promise.properRace(promises, count, results);
});
};
示例化版本:
_promise2.default.properRace(promises, count, results);