Urgent.News

What's breaking now, across thousands of outlets.

Tech

Synchronous, Asynchronous, and Promise in Javascript

1. Synchronous JavaScript Synchronous means code executes one line at a time, in order. The next line waits until the current line finishes. Example: console . log ( " First " ); console . log ( " Second " ); console . log ( " Third " ); Output : First Second Third Explanation: First line executes. Then the second line executes. Finally, the third line executes. Each statement runs in sequence.…

Synchronous JavaScript operates line by line, executing each instruction in order. The subsequent line waits until the current one concludes. For instance, console.log("First"); console.log("Second"); console.log("Third"); produces First Second Third. Each statement runs sequentially, with no overlap.

Asynchronous JavaScript permits operations to commence and conclude at varied intervals, enabling other code to execute concurrently. console.log("First"); setTimeout(function() { console.log("Second"); }, 2000); console.log("Third"); outputs First Third Second. "First" prints instantaneously. "setTimeout()" schedules the callback to execute after a minimum of 2 seconds. "Third" prints without waiting for the timer. "Second" prints once the timer callback runs.

A Promise embodies the eventual outcome of an asynchronous operation – success or failure. It exists in three states: Pending (operation incomplete), Fulfilled (success), or Rejected (failure). let myPromise = new Promise((resolve, reject) => { resolve("Task completed!"); }); myPromise.then(result => { console.log(result); }); Outputs Task completed!.

Here's a rejected Promise: let myPromise = new Promise((resolve, reject) => { reject("Something went wrong"); }); myPromise.catch(error => { console.log(error); }); Outputs Something went wrong.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

ListingForge

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 2: Side Hustle ListingForge ⚡ — The Open-Weight AI E-Commerce Listing Studio What I Built ListingForge is a local-first…

  • ListingForge AI studio converts raw images into optimized listings for major platforms.
  • Users create high-converting listings in under 30 seconds with built-in presets.
  • Platform offers subscription or lifetime license with zero marginal costs.

More from Thursday 15 October →