Recently, I have been seeing the concept of Promises being implemented in AngularJS and JQuery.
I have seen the implementation of a Futures in Java as in code below, however this requires concept of thread pools to be present in language/platform. However, there is no such threading concept in Javascript. How are Promises in Javascript implemented then ?
public class Futures1 {
private static final ExecutorService pool = Executors
.newFixedThreadPool(10);
public static void main(String[] args) {
Future<String> contentsFuture = null;
try {
contentsFuture = startDownloading(new URL("http://www.example.com"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
// other computation
try {
final String contents = contentsFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
public static Future<String> startDownloading(final URL url) {
return pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
try (InputStream input = url.openStream()) {
return IOUtils.toString(input, StandardCharsets.UTF_8);
}
}
});
}
}