Bug 1467571 [wpt PR 11385] - Make manifest's parsers quicker, a=testonly
[gecko.git] / devtools / shared / async-utils.js
blob3fd5316eaf9286a88180b4d098cd87c91738381c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 /**
8  * Helpers for async functions. Async functions are generator functions that are
9  * run by Tasks. An async function returns a Promise for the resolution of the
10  * function. When the function returns, the promise is resolved with the
11  * returned value. If it throws the promise rejects with the thrown error.
12  *
13  * See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
14  */
16 /**
17  * Adds an event listener to the given element, and then removes its event
18  * listener once the event is called, returning the event object as a promise.
19  * @param  Element element
20  *         The DOM element to listen on
21  * @param  String event
22  *         The name of the event type to listen for
23  * @param  Boolean useCapture
24  *         Should we initiate the capture phase?
25  * @return Promise
26  *         The promise resolved with the event object when the event first
27  *         happens
28  */
29 exports.listenOnce = function listenOnce(element, event, useCapture) {
30   return new Promise(function(resolve, reject) {
31     const onEvent = function(ev) {
32       element.removeEventListener(event, onEvent, useCapture);
33       resolve(ev);
34     };
35     element.addEventListener(event, onEvent, useCapture);
36   });