Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Maybe inconsistency? Even after years still some things are just cumbersome with async/await (or you sometimes have to resort for callbacks or explicit Promises)
Deno looks promising with its top level async/await
I think that new version of Node also supports it now, or was it only in REPL? -
@DJTM
Examples?
I use Observable as my primary async construct, so I have no dog in the fight per se. -
I hate working with an API that offers async/await/promise structure and thereby makes it impossible to use normal JavaScript structure for data processing.
Example Discord.JS -
DJTM144y@SortOfTested @SortOfTested
Using node-fetch
The async function;
async function getPlayerId(usernameInput){//function to fetch player id from name
const api_response = await fetch('http://example.com/api/players/...);
const data = await api_response.json();
//message.channel.send(data.players[0].playerId);
return(data.players[0].playerId);
}
The call to function;
let playerId = getPlayerId(q);
playerId.then(function(playerId) {
//code that uses API here
} -
@DJTM
Yeah, that's kind of fugly. There's a lot of pick apart there. This is how it looks in my world:
const getPlayerId$ = (username: string) => from(
fetch(`http://example.com/api/players/...}`)
).pipe(
map(response => response.json()),
map(({players}) => players && players[0].playerId)
);
// usage
function bindPlayer(player: Player, username: string){
combineLatest(of(player), getPlayerId$(username))
.pipe(
map(([player, playerId]) => //use player id here, return player),
).subscribe(
//(boundPlayer: player) => void here
);
} -
@stefanjarina
That's rxjs.
Angular would use HttpClient and not need to encapsulate the promise in a stream. -
@SortOfTested it never occured to me I could use rxjs outside of angular :-D need to try it out in some project. Thanks for an idea :-)
-
@DJTM I kinda expected you to have these problems.
I don't know any other JavaScript based API that fucks with async stuff as much as DJS
Related Rants
Small rant because I’m new here, but…I hate async functions in JavaScript.
Especially when dealing with an API.
random
api
javascript