webxdc / webxdc_docs

Documentation for Webxdc
https://docs.webxdc.org
10 stars 4 forks source link

document webxdc.sendToChat() #45

Closed r10s closed 1 year ago

r10s commented 1 year ago

@Simon-Laux can you push a commit that describes the usage of the promise? there is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then , however, in practise, this is still unclear to me - would we still need onFulfilled although that is never used? or is it just a .catch()?

ftr, this pr is more recent than https://github.com/webxdc/webxdc_docs/pull/44 which needs adaption afterwards as well. letting the user create native File or Blob objects was regarded as too complicated and error prone on first practical usages - apart from the additional overhead if data are already in base64 (not uncommon as updates do not allow blobs). in the future, we may enhance the function, however.

Simon-Laux commented 1 year ago

however, in practise, this is still unclear to me - would we still need onFulfilled although that is never used? or is it just a .catch()?

there are 3 ways:

A: try/catch statement with await (if you are inside of an async function, otherwise the wait keyword does not exist):

try {
 await my_promise
} catch (error) {
 /* handle the error, like logging it */
 console.log(error)
}

B: using catch:

my_promise.catch((error) => {
 /* handle the error, like logging it */
 console.log(error)
})

C: using then to set both callbacks (it don't really like the syntax):

my_promise.then(() => {}, (error) => {/* handle the error, like logging it */console.log(error)})
/* these could also work I guess? */
my_promise.then(null, (error) => {/* handle the error, like logging it */console.log(error)})
my_promise.then(undefined, (error) => {/* handle the error, like logging it */console.log(error)})
r10s commented 1 year ago

thanks a lot, @Simon-Laux , i think, for the example B: is the shortest and most intuitive variation, i will add a commit

r10s commented 1 year ago

i added a hint about versions, will merge that in to move forward