gpt4 book ai didi

javascript - 如何 promise Cordova 文件插件?

转载 作者:行者123 更新时间:2023-12-02 23:19:27 24 4
gpt4 key购买 nike

我想在 Apache Cordova 文件插件中 promise 一种方法,但我对“this”有疑问。

最初,我使用箭头函数,但当“this”问题开始出现时删除了这些函数。

    /**
* @param {Function} funcToPromisify -
* @param {*} firstArgForFuncToPromisify -
* @returns {Promise<any>} -
*/
const promisifyFunctionOne = function(funcToPromisify, firstArgForFuncToPromisify) {
return new Promise(function (resolve, reject) {
funcToPromisify(firstArgForFuncToPromisify, resolve, reject);
});
};


/**
* @param {Function} funcToPromisify -
* @returns {Promise<any>} -
*/
const promisifyFunctionTwo = function(funcToPromisify) {
return new Promise(function(resolve, reject) {
funcToPromisify(resolve, reject);
}
);
};


/**
* @param {string} pathToFile -
* @param {Function} urlResolverFunc -
* @param {object} stateObj -
* @returns {void} -
*/
const readFile = async function(pathToFile, urlResolverFunc) {
const fileEntry = await promisifyFunctionOne(urlResolverFunc, pathToFile);
console.log(fileEntry);
try {
const fileObj = await promisifyFunctionTwo(fileEntry.file);
let reader = new FileReader();
reader.onloadend = function() {
console.log('Successful file read: ' + this.result);
};
reader.readAsText(fileObj);
}
catch(error) {
console.log(error);
}
};

// Example of how readFile() is being called.
readFile(pathToFile, window.resolveLocalFileSystemURL);


我希望能够 promise 函数 FileEntry.file(),但我尝试此操作时出现此错误: TypeError: this.toInternalURL is not a function at FileEntry.file (FileEntry.js:82)

可以在此处查看该代码:https://github.com/apache/cordova-plugin-file/blob/74a46467a081a87bb69b3f2518cbb1db5375028f/www/FileEntry.js#L80

最佳答案

定义promisify时可以使用箭头函数。这是一个不依赖于上下文的基本示例 (this) -

const promisify = (f) =>
(...a) => new Promise ((res, rej) => f (...a, res, rej))

const basic = (a, b, c, success, failure) =>
c === 0
? failure (Error("c cannot be zero"))
: success (a + b + c)

promisify(basic)(1, 2, 3).then(console.log, console.error)
// 6

promisify(basic)(1, 2, 0).then(console.log, console.error)
// Error: "c cannot be zero"

现在对于需要上下文的函数,请观看我们如何使用 .bind 来保留上下文 -

const promisify = (f) =>
(...a) => new Promise ((res, rej) => f (...a, res, rej))

const account =
{ balance: 100
, withdraw: function (amount, success, failure)
{ if (amount > this.balance)
failure(Error("insufficient funds"))
else
(this.balance -= amount, success(this))
}
}

const withdraw =
promisify(account.withdraw.bind(account))

withdraw(35).then(console.log, console.error)
// { balance: 65, withdraw: fn... }

withdraw(9999).then(console.log, console.error)
// Error: "insufficient funds"

<小时/>

请注意,这与节点样式的 promisify 不同,其中异步函数仅接受一个 (1) 回调,并且错误始终作为回调的第一个参数传递 -

const promisify = f =>
(...a) => new Promise ((res, rej) =>
f (...a, (e, x) =>
e ? rej(e) : res(x)
)
)

const divide = (a, b, nodeCallback) =>
b === 0
? nodeCallback(Error("cannot divide by zero"))
: nodeCallback(null, a/b)

const pdivide =
promisify(divide)

pdivide(10,2).then(console.log, console.error)
// 5

pdivide(10,0).then(console.log, console.error)
// Error: "cannot divide by zero"

注意,最后一个代码段中定义的 promisify 包含在 Node 中,作为 util.promisify

关于javascript - 如何 promise Cordova 文件插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com