Transfer Complex Data Types including Promises and Async Functions
mainBIDC supports a wide range of JavaScript types, including Date, RegExp, Map, Set, ArrayBuffer, TypedArray, and most importantly, Promise and Async Function.
When you transfer a Promise, the receiving side can await it. When you transfer an Async Function, the receiving side can call it as if it were local, and the execution will occur in the original context.
// Sending side
const response = await send({
date: new Date(),
map: new Map([['key', 'value']]),
set: new Set([1, 2, 3]),
arrayBuffer: new Uint8Array([1, 2, 3]).buffer,
promise: Promise.resolve('resolved value'),
function: async (x) => {
await sleep(1000)
return x * 2
}
})
// Receiving side
receive(async (payload) => {
const resolvedValue = await payload.promise
const result = await payload.function(5)
return { status: 'success' }
})