JSFiddle - React, Tailwind, and code Playground

Accedo job interview test prototype 4

by Csaba Hellinger

HTML

<div ng-app="app">
    <div ng-controller="sumController">
        <div ng-show="!result" class="waiting">Waiting...</div>
        <div ng-show="result">
            <div class="result">Result: {{result}}</div>
            <div class="rejected">Rejected: {{rejected}}</div>            
        </div>
    </div>
</div>

CSS

.waiting, .rejected {
    color: gray;
}

.result {
    font-size: 1.4em;
}

JavaScript

// Change some of the values to null in 'data', 
// then modify the code to handle (count) rejected promises.
// - You can only modify the code between the START and END comments.
// - You don't have to keep the get calls sequential, you can make it parallel.

angular.module('app', [])
	.service('dataService', function dataService($q, $timeout) {
    	var data = {'a': 1, 'b': 2, 'c': 3};
    
        function get(key) {
            var deferred = $q.defer(),
                value = data[key];
            $timeout(function() {
            	if (value) {
                	deferred.resolve(value);
                } else {
	                deferred.reject();
                }                
            }, 100);
            return deferred.promise;
        }
        
        return {
        	get: get
        };
    })
    .controller('sumController', function sumController($scope, dataService) {
    
    	$scope.result = 0;
    	$scope.rejected = 0;

        // START --------------------------------------------------------

		var a, b, c;
        dataService.get('a')
            .then(function (data) {
                a = data;
                return dataService.get('b');
            })
            .then(function (data) {
                b = data;
                return dataService.get('c');
            })
            .then(function (data) {
            	c = data;
                $scope.result = a + b + c;
            });

        // END ----------------------------------------------------------

    });