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/. */
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.
13 * See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
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
22 * The name of the event type to listen for
23 * @param Boolean useCapture
24 * Should we initiate the capture phase?
26 * The promise resolved with the event object when the event first
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);
35 element.addEventListener(event, onEvent, useCapture);