How do I use promises in a Chrome extension?

前端 未结 2 356
广开言路
广开言路 2020-12-20 07:29

What I am trying to do is create a chrome extension that creates new, nested, bookmark folders, using promises.

The function to do this is chrome.bookmarks.creat

2条回答
  •  眼角桃花
    2020-12-20 08:00

    Take the advantage of the convention that the callback function always be the last argument, I use a simple helper function to promisify the chrome API:

    function toPromise(api) {
      return (...args) => {
        return new Promise((resolve) => {
          api(...args, resolve);
        });
      };
    }
    

    and use it like:

    toPromise(chrome.bookmarks.create)(bookmark).then(...);
    

    In my use case, it just works most of the time.

提交回复
热议问题