Javascript(typescript)Chrome扩展,函数回调作为承诺?

时间:2017-04-05 06:40:03

标签: javascript typescript google-chrome-extension chain arrow-functions

代表这样的代码

let anotherFolder='whatever';
let anotherFolder2='whatever';

chrome.bookmarks.create( {title:'whatever2'}, function( parentFolder ) {
  chrome.bookmarks.move( anotherFolder, {parentId: parentFolder.id}, function() {
    chrome.bookmarks.removeTree( anotherFolder2, function() {
      resolve();
    });
  });
});

我可以将它转换为连锁功能吗?像

这样的东西
let anotherFolder='whatever';
let anotherFolder2='whatever';

return new Promise(function(resolve){
  chrome.bookmarks.create( {title:'whatever2'}, function( parentFolder ) {
    resolve(parentFolder);
  }).then( (parentFolder) => {
    chrome.bookmarks.move( anotherFolder, {parentId: parentFolder.id}, function() {
      resolve();
    })
  }).then () => {
    chrome.bookmarks.removeTree( anotherFolder2, function() {
      resolve();
    });
  });
});

因为我收到了'属性'然后'在类型' void'。'

上不存在

*这是javascript-typescript,但它不应该在这里(?)

2 个答案:

答案 0 :(得分:4)

你需要“宣传”任何chrome.bookmarks.*函数 - 这显然使它们与Firefox中的browser.bookmarks.*函数和(最终)Edge Web Extensions一样工作

宣传您使用的功能

let create = p1 => new Promise((resolve, reject) => {
    chrome.bookmarks.create(p1, resolve);
});
let move = (p1, p2) =>  new Promise((resolve, reject) => {
    chrome.bookmarks.move(p1, p2, resolve);
});
let removeTree = p1 => new Promise((resolve, reject) => {
    chrome.bookmarks.removeTree(p1, resolve);
});

然后您可以按如下方式使用这些功能:

let anotherFolder='whatever';
let anotherFolder2='whatever';


create({title:'whatever2'})
.then(parentFolder => move(anotherFolder, {parentId: parentFolder.id}))
.then(() => removeTree(anotherFolder));

如果您考虑在Firefox(最终是Edge)中使用您的扩展程序

未经过测试

let create = browser.bookmarks.create || p1 => new Promise((resolve, reject) => {
    chrome.bookmarks.create(p1, resolve);
});
let move = browser.bookmarks.move || (p1, p2) => new Promise((resolve, reject) => {
    chrome.bookmarks.move(p1, p2, resolve);
});
let removeTree = browser.bookmarks.removeTree || p1 => new Promise((resolve, reject) => {
    chrome.bookmarks.removeTree(p1, resolve);
});

然后上面的代码应该在任何Web扩展

中工作

答案 1 :(得分:4)

接受的答案很好,但有些图书馆已经为你做了这些,例如chrome-extension-async。对于有效的回调,还包括TypeScript definitions

用凉亭安装它

bower install chrome-extension-async

npm

npm i chrome-extension-async

download chrome-extension-async.js文件并直接加入:

<script type="text/javascript" src="chrome-extension-async.js"></script>

您可以将此库与promises一起使用,它允许您链接函数(而不是嵌套回调):

function whatever(anotherFolder, anotherFolder2) {
    return  chrome.bookmarks.create({title:'whatever2'}).
        then(parentFolder => 
            chrome.bookmarks.move(anotherFolder, {parentId: parentFolder.id})).
        then(() => 
            chrome.bookmarks.removeTree(anotherFolder2));
}

我强烈建议您使用(相当新的)asyncawait语法,如果需要,TypeScript可以将其转换为旧版本的JS。 然后你的代码变成:

async function whatever(anotherFolder, anotherFolder2) {
    const parentFolder = await chrome.bookmarks.create({title:'whatever2'});
    await chrome.bookmarks.move(anotherFolder, {parentId: parentFolder.id});
    await chrome.bookmarks.removeTree(anotherFolder2);
}

这与基于promise的代码完全相同,但是使异常更新并使其更容易与逻辑流一起使用(即使使用promises,if分支逻辑也可能很混乱。