Jump to content

Is javascript promise the same as an arraylist in java?

babadoctor
Go to solution Solved by reniat,

Apples and oranges. a promise in JS seems to be a way to write asynchronous code (i've never written concurrent JS, so i could be wrong), while an arraylist in java is just an implementation of an abstract data type. The only semi-related aspect of arraylist is that there are concurrency concerns in that arraylists are not synchronized so you need to be careful if you have multiple threads operating on the same list.

Title

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to comment
Share on other sites

Link to post
Share on other sites

Apples and oranges. a promise in JS seems to be a way to write asynchronous code (i've never written concurrent JS, so i could be wrong), while an arraylist in java is just an implementation of an abstract data type. The only semi-related aspect of arraylist is that there are concurrency concerns in that arraylists are not synchronized so you need to be careful if you have multiple threads operating on the same list.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 18/07/2018 at 4:35 AM, reniat said:

Apples and oranges. a promise in JS seems to be a way to write asynchronous code (i've never written concurrent JS, so i could be wrong).

Promises are like callbacks, it means that part of the code will be executed only when the promise is resolved.

 

Bare with me I'm on my phone, so say you were using nodeJs and making an api request

 


const got = require('got');

const results = got.get('http://example.com/data/123');

console.log(results)

 

Because the got request is async it will fire of the get request then move on to the console.log and you'll get undefined.

 

Now got returns a promise so

 


const got = require('got');

got.get('http://example.com/data/123').then((results) =>{

    console.log(results)

})

 

 

This will cause the event loop to wait until the promise is resolved before doing the console.log

 

What is great about promises is the  you can chain .then and what ever is returned from that function is passed to the next one.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×