As a natural consequence of the heavy computation of machine learning algorithms, asynchronous data access is inevitable if we wish to keep our application efficient and working interactively. In JavaScript, asynchronous execution is often implemented with a Promise object. A promise represents an asynchronous operation that ends in either success or failure. Most of the operations that download data from tensors return a Promise object, which ensures that the user fetches the data once it is ready.
To return a Promise object, we need to declare the function as an async. For instance, the Tensor.data method returns a Promise that computes TypedArray, which contains the data's results:
async data<D extends DataType = NumericDataType>(): Promise<DataTypeMap[D]> {
// Do something to return the value.
// ...
return data as Promise<DataTypeMap...