import AbstractAsyncService from './AbstractAsyncService';
import Deferred, { DeferredError } from '../../Utils/Async/Deferred';

export default class AsyncService extends AbstractAsyncService {
    create(name) {
        return new Deferred(name).promise;
    }

    get(name) {
        return Deferred.get(name);
    }

    remove(name) {
        Deferred.get(name).remove();
    }

    resolve(name, result) {
        try {
            Deferred.get(name).resolve(result);
        } catch (e) {
            if (e.type !== DeferredError.type) {
                throw e;
            }
        }
    }

    reject(name, error) {
        try {
            Deferred.get(name).reject(error);
        } catch (e) {
            if (e.type !== DeferredError.type) {
                throw e;
            }
        }
    }
}
