Can D.O.H be used to test a sequence of asynchronous function calls?

I have been experimenting with using doh.Deferred to create a test that verifies the following sequence of events:

  1. Login with user A (asynchronous)
  2. Log out (synchronous)
  3. Login again with user A (asynchronous)

The second callback function's return value is another doh.Deferred object. I expected the callback chain of d to wait for d2, but that doesn't happen. The test completes before d2.callback is triggered.

Can anyone point out where I might be making a mistake in my approach?

Is there a more effective way for me to conduct this test?

function test() {
    var d = new doh.Deferred();

    d.addCallback(function() {  
        Comm.logout(); /* synchronous */
        try {   
            // validate using doh.t and doh.is
            return true;
        } catch (e) {
            d.errback(e);
        }
    });

    d.addCallback(function() {
        var d2 = new dojo.Deferred();
        /* asynchronous - third parameter serves as a callback */
        Comm.login('alex', 'asdf', function(result, msg) {
                try {
                    // validate using doh.t and doh.is
                    d2.callback(true);
                } catch (e) {
                    d2.errback(e);
                }                   
            });
        return d2; // returning doh.Defferred -- expecting d to wait for d2.callback
    });     

    /* asynchronous - third parameter serves as a callback */
    Comm.login('larry', '123', function (result, msg) {
        try {
            // validate using doh.t and doh.is 
            d.callback(true);
        } catch (e) {
            d.errback(e);
        }
    }); 

    return d;
}

Answer №1

Success! The issue was related to the scope of d2.

function validate() {
    var x = new validation.Validate();
    var y = new validation.Validate();

    x.addValidation(function() {  
        Authentication.logout(); /* synchronus */
        try {   
                // perform validations
                return true;
        } catch (error) {
                x.errorCallback(error);
        }
    });

    x.addValidation(function() {
        /* async - third parameter is a callback */
        Authentication.login('user1', 'pass123', function(result, message) {
                        try {
                                // perform validations
                                y.callback(true);
                        } catch (error) {
                                y.errorCallback(error);
                        }                                       
                });
        return y; // waiting for y.callback
    });         

    /* async - third parameter is a callback */
    Authentication.login('user2', 'password456', function (result, msg) {
        try {
                // perform validations
                x.callback(true);
        } catch (error) {
                x.errorCallback(error);
        }
    }); 

    return x;
}

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

Tips on transitioning a Node.js application from JavaScript to TypeScript incrementally

I have a JavaScript node application that has grown quite large and I am considering migrating to TypeScript for faster development and easier code maintenance. I have installed TypeScript along with the node and mocha types using the following commands: ...

Implementing pagination with complete page information using node.js and mongodb

Hello, I am currently working on implementing pagination in NodeJS and MongoDB. Check out the code I have written below: router.get("/PO_pagination", verify, async (req, res) => { console.log("req.query", req.query) try { let ...

Express.js throws an error when trying to access req.session when it

I've searched through multiple posts on this topic, however, none were able to resolve my issue. Below is the code snippet from my server.js file: var express = require('express'); var app = express(); app.configure(function(){ app.set ...

Step-by-step guide for implementing a scroll event on FancyBox

I am facing an issue with a large fancybox popup that contains a scroll function. I need to find a way to attach an event to this fancybox popup when scrolling. I have tried several different events, such as: $('.fancybox-inner').off("scrol ...

Combining 2 dates into a single cell format

I'm struggling to change the date format of two dates displayed in one cell using ag-grid. I have tried creating a new function called dateFormatterr with two parameters, but it doesn't seem to work. Below is a snippet of my code and a screenshot ...

Combining jQuery with Ubiquity: Fetching a document object from a specific URL

Currently, I am in the process of developing a straightforward Ubiquity command that will execute a query on Wolfram Alpha and exhibit the results within the Ubiquity preview component. To achieve this, it is essential to configure the innerHTML of the pr ...

AngularJS $http.post() response function not executing in the correct sequence

When calling a function from my angular controller to make a $http.post() request, the code below the function call is executing before the successFunction(), preventing the code inside the if block from running. How can I ensure the if block executes wi ...

Deleting Single Files from a Selection of Multiple Files With File Input

Is there a way to remove specific files from the list of files selected for upload when multiple files can be chosen by the user? As an example, consider this input field: <input type='file' name='file' id='file' multiple= ...

Performing asynchronous ajax calls with jQuery

Here is some code I have that involves a list and making an ajax call for each element in the list: util.testMethod = function(list) { var map = new Map(); list.forEach(function(data) { $.ajax({ ...

Issue encountered with AJAX multiple image uploader

I'm attempting to create an PHP and JavaScript (jQuery using $.ajax) image uploader. HTML: <form method="post" action="php/inc.upload.php" id="upload-form" enctype="multipart/form-data"> <input type="file" id="file-input" name="file[]" a ...

Encountered an Angular SSR error stating "ReferenceError: Swiper is not defined"

When attempting to implement SSR (Server-Side Rendering) in a new project, everything runs smoothly and without issue. However, encountering an error arises when trying to integrate SSR into an existing project. https://i.sstatic.net/QOI6A.png ...

Changing colors in the rows of a table

Here is a fiddle I created to demonstrate my issue. https://jsfiddle.net/7w3c384f/8/ In the fiddle, you can see that my numbered list has alternating colors achieved through the following jQuery code: $(document).ready(function(){ $("tr:even").css("ba ...

Is it possible to customize eslint rules for a create-react-app that has been ejected?

Currently, I am in the process of developing an application using React and utilizing NPM. The configuration of my package.json file is as follows: { "name": "mouseflow-react", "version": "0.1.0", "private": true, // Dependencies section with var ...

Using jQuery date picker to only allow selection of Sundays that fall on even weeks

Is there a way to disable Sundays if it is an even week of the year using this code? function settings(date) { if (date.getDay() == 0) { return [true, "", "Works"]; } else { return [false, "", ""]; } } $i("#searchsectionbari ...

difficulty encountered with page.registerstartupscript function

I've been using page.registerstartupscript in my C# code behind. Here's an example: string item1="category1"; string Script = "window.program = '" + item1 + "';"; Page.RegisterStartupScript("PopupScript", Script); The variable ite ...

Issue with Bootstrap Navbar dropdown functionality not functioning correctly in browsers, but working fine on Codepen

I'm encountering an issue with a dropdown menu in the navbar of my document. The dropdown menu works fine in my codepen but not in my text editor (Sublime). I've tried various solutions and couldn't find a fix, so I'm reaching out here ...

The reliability of next router events can sometimes be called into question as they do not always function consistently

I've been working on creating a loading screen for my Next.js project. The issue I'm facing is that sometimes the loading message stays on the screen and doesn't go away even after the page has loaded. I suspect this may be due to the state ...

View a pink map on Openlayers 2 using an iPhone

I am currently working on a project where I am trying to track my location using my smartphone and display it on a map. To achieve this, I am utilizing openlayers 2. However, I am encountering an issue. When I implement the code below in a Chrome Browser ...

Tips for automatically refreshing the browser when serving static assets through Express during development

Is there a way to set up automatic browser refresh when making changes to client files in ReactJS? I have an Express server sending static assets from the public directory on a GET request to "/". This is how my server code looks: const express = require ...

Issue with adding object to array using forEach() function

As I navigate my way through an express route, I am puzzled as to why the "purchasedCards" array turns out empty after going through these database calls. Despite successfully gathering all the necessary information from various DB Queries and placing it i ...