You are not fulfilling the contract of >>=
(which, as you so rightly point out, Promise
mostly fulfills). Your second function (item.client
) is not a -> m b
but a -> void
. Since this is JS, we just go on to the next >>=
call the minute item.client
completes (because, right). Change the type of your second function and your third will follow.
// Assuming that $.post is a recent version of jQuery it also returns promises
// as long as you don't supply the callback parameter
.then(() => {
item.client.personId(...);
// Returning a new promise here properly types this function as `a -> m b`
return $.post('/api/clients', clientJson)
.then(resp => item.client = ko.mapping.fromJS(resp));
})
Of course, as @ben_lubar points out, you can also just use async
/ await
:
const fromJS = ko.mapping.fromJS;
const toJSON = ko.toJSON;
async function storeItem(item) {
item.person = fromJS(
await $.post('/api/people/' + item.person.id(), toJSON(item.person))
);
item.client.personId(item.person.id());
item.client = fromJS(
await $.post('/api/clients/', toJSON(item.client))
);
item.demographics.personId(item.client.id());
item.demographics = fromJS(
await $.post(...)
);
}