Title: Theory / Important / CS / Programming / JS / Mozilla / Async & Promises
Tags: automatedNewLineAfterCommaSpace seen_description declaration todo_declaration computer_to_instruction

Function: then(successCallback, failureCallback) executed in case of success/failure
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Name: Promise
Axiom: arguments of then are never executed before the current run is completed
Axiom2: callbacks are invoked if the current run complete, even if it completes before the callbacks are added
Intuition: an object representing the eventual completion or failure of an asynchronous operation.
a returned object to which you attach callbacks, instead of passing callbacks into a function.
Axiom3: If then is applied multiple time, all callbacks are executed in the order in which they are added
Language: js

Name: A promise chain
Definition: promise.then(success_1, failure_1)....then(success_n, failure_n)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Instruction: pr.then(successCallBack, failureCallBack)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A promise.
If pr succeed with value, then execute as a promise successCallBack(value)
If pr fails with reason, then execute as a promise failureCallBack(reason)

Instruction: pr.then(CallBack)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A promise to execute pr, returning value, and then executing CallBack(value).
Failing only if one of those fails

Instruction: pr.catch(CallBack)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A promise.
If pr succeed, return pr's value.
Otherwise, it it failed with reason execute CallBack(reason) and return its value or its reason to fail

Name: rejectionhandled
Definition: Event sent when a promise reject and the rejection is handled by the executior reject function
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Context: event

Name: unhandledrejection
Definition: Event sent when a promise reject and no rejection handler is available
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Context: event

Instruction: funPromise = args => new Promise(
  callback=> fun(callback, args)
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A function which return a promise to execute fun on arguments and then the callback
Variables: fun a function taking a callback and other arguments args

Instruction: Promise.resolve(
value
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A resolved promise with value value

Instruction: Promise.reject(reason)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: Javascript
Returns: A rejected promise with reason reason

Instruction: Promise.all(
  [f_1,
  ...,
  f_n]
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Language: js
Returns: A promise that execute all f_i in parallel that returns the array of results, and fails if any fail

Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Implementation: [f_1, ..., f_n].reduce((p, f)=> p.then(f), Promise.resolve())
Language: js
Returns: A promise that execute all f_i sequentially, with the result returned by f_i-1 and fail if any fail

Question: When to use promise inside of promise?
Answer: Never if possible
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Name: A pending promise
Definition: A promise in initial state, neither fulfilled nor rejected.
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Name: A fulfilled promise
Definition: A promise whose operation was completed succesfully
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Name: A rejected promise
Definition: A promise whose operation failed
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Name: States of a promise
Definition: Pending
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Definition2: fulfilled
Conjdef: Either
Definition3: rejected

Instruction: new Promise(f)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Language: js
Returns: A promise, executing f(resolve, reject), where resolve and reject are function to call if the promise resolve/reject with the value

Instruction: Promise.allSettled(
  [promise_1,
  ...,
  promise_n]
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Language: js
Returns: A promise, succeeding with values the tuple of values/reasons of each promise_i when all are settled

Instruction: Promise.any(
  [promise_1,
  ...,
  promise_n]
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Language: js
Returns: A promise, with the value of the first fulfilled promise_i

Instruction: Promise.race(
  [promise_1,
  ...,
  promise_n]
)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Language: js
Returns: A promise which is equal to the first promise of the list l = {promise_1, ..., promise_n} which resolves or reject
Variables: l = {promise_1,
  ..., promise_n}: a list of promises

Instruction: Promise.resolve(promise)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Language: js
Returns: A promise identical to promise

Question: How to deal with values v when we don't know whether they are promise or not?
Answer: Use Promise.resolve(v), to ensure it's a promise
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Instruction: pr.then(callback, callback)
Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Effect: pr.finally(callback)
Language: js
Variables: pr a Promise

Name: Event queue
Definition: A queue of operations that are going to be run when the current main thread has finished processing
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing
Context: JS

Name: Callback hell
Definition: Callback nested in other callback, leading to code whose execution is from bottom to top
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing

Name: Blocking code
Definition: Some code/function that must be executed entirely before allowing more functions to be executed
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Concepts

Name: Main thread
Definition: can access the dom
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Concepts
Definition2: get user events
Conjdef: The only thread that

Name: Worker thread
Definition: Any thread which is note the main thread
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Concepts
Context: JS

Instruction: setTimeout(
  foo,
  n,
  par1,
  ...,
  parn
)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Put foo(par1, ..., parn) in the work queue, to execute in at least n miliseconds
Language: JS
Returns: An unique id referring to this timeout

Instruction: clearTimeout(to)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: The timeout with id to is cancelled. I.e. not executed if it's not already started
Language: JS
Variables: to a timeout id

Instruction: setInterval(
  foo,
  n, par1,
  ..., parn
)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Put foo(par1, ..., parn) in the work queue, to execute infinitely often, with at least n miliseconds between each start of execution
Language: JS
Returns: An unique id referring to this timeout

Instruction: clearInterval(si)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Stop executing new occurences of the setInterval action
Language: JS
Variables: si an id of setInterval

Question: How to run a function f infinitely often, with m milisecond between the end of an execution and the start of a new one.
Answer: Put another set time out at the end of f which calls f recursively (setInterval count the difference of time between two start of the function call)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Extra: Settime

Question: How to run a function f infinitely often, with various time between two calls.
Answer: Put another set time out in f which calls f recursively, recalculating the new time
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals

Instruction: setTimeout(
  foo,
  0, par1,
  ..., parn
)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Execute foo(par1, ..., parn) at the end of current thread
Language: js
Returns: An id of this timeout

Instruction: requestAnimationFrame(foo)
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Execute foo(timestamp) before the screen is repaint
Language: js

Name: Dropping frame
Definition: The fact that, if it takes too much time to compute a frame to display, the next frame will be dropped in order to let the long frame's computation to end
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Context: js
Name2: Jank

Instruction: cancelAnimationFrame(rAF);
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Effect: Clearing the requestAnimationFrame() with id rAF
Language: js

Instruction: await P
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
Effect: Wait until P is resolved
Language: js
Returns: the value to which P is resolved
Variables: P a promise

Question: How to await for multiple values simultaneously, letting their computations runs in parallel.
Answer: Put then in Promise.all and wait for the All
Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
Variables: js

Name: Dedicated worker
Definition: workers that are utilized by a single script
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: js

Name: Shared worker
Definition: Workers that can be utilized by multiple scripts running in different windows, IFrames, etc.
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: js

Name: Service worker
Definition: proxy servers that sit between web applications, the browser, and the network (when available).
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: js
Extra: They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs.

Name: Chrome workers
Definition: Firefox-only type of worker that you can use if you are developing add-ons and want to use workers in extensions and have access to js-ctypes in your worker
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: js

Instruction: new Worker("foo.js")
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Language: js
Returns: A worker executing foo.js

Variable: onmessage
Meaning: A function called with message received by a worker
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: worker
Language: js

Instruction: w.postMessage(foo)
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Effect: Enqueue message foo to worker w
Language: js
Context: worker

Instruction: new SharedWorker("foo.js")
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Language: js
Context: worker
Returns: A shared worker for foo.js, potentially an already existing one

Instruction: sw.port
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Language: js
Context: shared worker
Returns: A MessagePort to communicate with the shared worker

Instruction: onmessageerror
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Language: js
Context: shared worker port
Returns: An EventListener called when a MessageEvent of type MessageError is fired—that is, when it receives a message that cannot be deserialized.

Variable: onconnect
Meaning: A function, taking message e. Answer should be posted on e.ports[0], input is in e.data
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: Shared worker
Language: js

Variable: onmessageerror
Meaning: An EventListener called when a MessageEvent of type MessageError is fired—that is, when it receives a message that cannot be deserialized.
Page: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
Context: Shared worker
Language: js

Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
Syntactic sugar: async function f(params) => {
  bar
}
Implementation: function f(params) {
  const aux = (params) => {    bar
  }
  return new Future(
    (success) => {
      success(aux(params));
    }
  )
}
Language: js

Page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
Syntactic sugar: async function(params) {
  bar
}
Implementation: function (params) {
  const aux = (params) => {    bar
  }
  return new Future(
    (success) => {
      success(aux(params));
    }
  )
}
Language: js
Abbreviation: async (params) => {
  bar
}