I have created a simply function with sub functions that returns balance and you can add an amount to update your balance.
const bankAccount = (initialBalance) => {
let balance = initialBalance;
return {
getBalance: function() {
return balance;
},
deposit: function(amount) {
balance += amount;
return balance;
},
};
};
const account = bankAccount(100);
account.getBalance();
account.deposit(10);
My question is I want to make this function asynchronous, my question is should the overarching function be wrapped in a promise or do the sub functions need to be wrapped in a promise.
This is the kind of approach I was thinking. Is this correct?
async function bankAccount(initialBalance) {
let balance = initialBalance;
return await new Promise((resolve, reject) => {
if ("some error") {
reject("something went wrong");
}
return {
getBalance: function () {
resolve(balance);
},
deposit: function (amount) {
balance += amount;
resolve(balance);
},
};
});
}