The callback function seems to be experiencing issues with the await functionality

I have a scenario where I am using two functions. In this case, I am calling cbf() from func() via callback, and although I am using await, the output of after callback is displayed before the callback function.

function cbf(name, callback: Function) {
    console.log(name)
    callback("123")
}

function async func() {
    await cbf("alice", function(aa) {
        console.log(aa)
    })
    console.log("after callback")  
}

Answer №1

Consider implementing Promise

function asynchronousFunction(input) { 
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(input);
    }, 2000);
  });
}

async function performOperation() {
  var result = await asynchronousFunction(10);
  console.log(result); // 10
}

performOperation();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Substitute terms with the usage of array map

Here is an array for you (2) ['beginning=beginner', 'leaves=leave'] as well as a string its beginner in the sounds leave that has been converted to an array using the following code var words = text.split(' '); My goal ...

How can I prevent the same JavaScript from loading twice in PHP, JavaScript, and HTML when using `<script>'?

Is there a PHP equivalent of require_once or include_once for JavaScript within the <script> tag? While I understand that <script> is part of HTML, I'm curious if such functionality exists in either PHP or HTML. I am looking to avoid load ...

React window resizing actionsWould you like some more information on this

I am attempting to create a resize function that will retrieve the window width and dynamically display it using react. Here is my current code: class Welcome extends React.Component { constructor() { super(); this.state = { ...

There is no image to be found in the Next Js React project

My navbar component is displaying a logo perfectly in the development environment, as well as in the production build when viewed locally. However, once it is uploaded to Netlify, the image randomly goes missing. It's interesting to note that another ...

How can JavaScript pass a variable through the URL?

I am attempting to pass a variable through the URL: http://localhost/new_wiki/test.php?id=http://example.com In my code, I have var first = getUrlVars()["id"];. This line is supposed to pass the value but it doesn't seem to be working. Can someone pl ...

What is causing the error message "Uncaught TypeError: Cannot read property 'helpers' of undefined" to occur in my straight-forward Meteor application?

As I follow along with the tutorial here, I encounter an issue after replacing the default markup/code with the provided HTML and JavaScript. The error message "Uncaught TypeError: Cannot read property 'helpers' of undefined" appears. Below is t ...

Writing this SQL query in Sequelize

I am attempting to write this SQL query in my Sequelize controller, but I need to ensure that the data_time is within the last 30 days. SQL: SELECT * FROM bankapplication_transactions WHERE id_sender = 1 OR id_recipient = 1 AND data_time BETWEEN NOW() - ...

Leveraging LevelGraph with MemDOWN

I've been experimenting with using LevelGraph and MemDOWN together, but I've noticed that my put and get queries are significantly slower compared to using the filesystem directly with LevelUP. It seems like there might be some mistake in my setu ...

Guide to installing angular-ui-bootstrap through npm in angularjs application

My goal is to incorporate angular-ui-bootstrap into my project using the following steps: 1. npm install angular-ui-bootstrap 2. import uiBootstrap from 'angular-ui-bootstrap'; 3. angular.module('app', [      uiBootstrap    ]) I ...

"Navigate back to a previous page in Vue Router without having to

I am currently exploring the option of creating a back button in my Vue.js application using vue-router that mimics the behavior of the browser's native back button. The challenge I'm facing is that when using history mode for the router and tryi ...

Issue found in factory and service dependencies

To retrieve user information, the factory sends a request to the API using ApiRequest.sendRequest: (function() { angular.module('isApp.user', []) .factory('UserProfileFactory', function( $log, ApiRequest, dataUrls ) { // User pr ...

How to unload and remove an object imported with OBJLoader in Three.js

I'm facing a small issue that I can't seem to figure out. I've successfully added an object to my scene using OBJLoader, but now I need to remove it. I've tried clearing the scene.children with code, but it doesn't delete the "flow ...

Crafting an interactive SVG element that maintains its clickability without interfering with mouseLeave events

Trying to Achieve: Changing color and displaying SVG when hovering over a row Clicking the SVG triggers a function, including external functions (not limited to ones defined inside script tags) Returning the row to its original form when mouse leaves Cha ...

Is it possible to send variables within an ajax request?

I'm currently in the process of building my own website, and I have a specific requirement where I need to display different queries based on which button is clicked. Can this be achieved using the following code? Here's the HTML snippet: <d ...

Tips for Making JQuery UI Droppable Work with Multiple Elements

I've been tasked with modifying a piece of code that currently allows users to drag one row from a table to a div. The new requirement is to enable users to select multiple rows and drag them all to the div. I'm struggling to tweak the existing c ...

The data type 'T' cannot be assigned to type 'T'

Having extensive experience as a javascript developer, I recently delved into learning C# as my first statically typed language. My upcoming project involves using TypeScript, so I've been refreshing my knowledge on it. Below is the code I have writt ...

Struggling to destructure props when using getStaticProps in NextJS?

I have been working on an app using Next JS and typescript. My goal is to fetch data from an api using getStaticProps, and then destructure the returned props. Unfortunately, I am facing some issues with de-structuring the props. Below is my getStaticProp ...

Interactive image grid with adjustable description field per image upon selection

My goal is to create a grid of images with a single text field below the grid. This text field should display the description of the image that was last clicked. The grid is implemented using floating divs within a main div, as shown in the code snippet be ...

Encountering a problem when utilizing the each loop within an ajax request

While attempting to iterate through a each loop within an Ajax call, I encounter the following error: TypeError: invalid 'in' operand e Here is my Ajax call code snippet: $.ajax({ type: "POST", url: "/admin/counselormanagem ...

The class functions perfectly under regular circumstances but ceases to operate once it is initialized

I'm currently developing a bluetooth remote in React Native. The issue I am facing is that my BLE class works perfectly on its own, but certain sections of code seem to malfunction when implemented within another class. import BLE from './Core/BL ...